Monday, August 1, 2022

Python CSV connectivity with no extra column added - Project Class XII CBSE

Important Python code to import and export dataframe to CSV file for Project Class XII CBSE

 

writing DataFrame to CSV File

data = {
  "name": ['RAM', 'SHYAM', 'SITA'],
  "calories": [420, 380, 390],
  "duration": [50, 40, 45]
}
df = pd.DataFrame(data)
print(df)
df.to_csv("devendra_test.csv")

Output of print(df) statement 
 

This is how file will look like in CSV File   


Reading CSV File into Python DataFrame
df = pd.read_csv("devendra_test.csv")
print(df)

Output of print(df) statement 

Reading CSV File into Python DataFrame with no Extra Columns Added

df = pd.read_csv("devendra_test.csv",\
                 usecols=["name", "calories","duration"],\
                 dtype={"name": str, "calories":np.int32, "duration": np.float32})
print(df)
Again Output of print(df) statement 
 
 
 

No comments:

Post a Comment