After working on a dataset and doing all the preprocessing we need to save the preprocessed data into some format like in csv , excel or others.
This python source code does the following :
1. Creates data dictionary and converts it into dataframe
2. Saves it in CSV format
So this is the recipe on how we can save Pandas DataFrame as CSV file.
import pandas as pd
We have only imported pandas which is needed.
We have created a dictionary of data and passed it in pd.DataFrame to make a dataframe with columns 'first_name', 'last_name', 'age', 'Comedy_Score' and 'Rating_Score'.
raw_data = {'first_name': ['Sheldon', 'Raj', 'Leonard', 'Howard', 'Amy'],
'last_name': ['Copper', 'Koothrappali', 'Hofstadter', 'Wolowitz', 'Fowler'],
'age': [42, 38, 36, 41, 35],
'Comedy_Score': [9, 7, 8, 8, 5],
'Rating_Score': [25, 25, 49, 62, 70]}
df = pd.DataFrame(raw_data, columns = ['first_name', 'last_name', 'age',
'Comedy_Score', 'Rating_Score'])
print(df)
So now we have to save the dataset that we have created. We save it in many format, here we are doing it in csv and excel by using to_csv and to_excel function respectively.
df.to_csv('raw_data.csv', index=False)
df.to_excel('raw_data.xls', index=False)
So the output comes as two saved file one in csv format and other in excel format.
first_name last_name age Comedy_Score Rating_Score 0 Sheldon Copper 42 9 25 1 Raj Koothrappali 38 7 25 2 Leonard Hofstadter 36 8 49 3 Howard Wolowitz 41 8 62 4 Amy Fowler 35 5 70