Save Pandas dataframe to csv and excel file
Use “to_csv” and “to_excel” methods to save pandas dataframe to csv and excel file respectively
# Imports
import pandas as pd
# Let's create a pandas dataframe
df = pd.DataFrame({"Name": ['Joyce', 'Joy', 'Ram', 'Maria'],
"Age": ['19', '18', '20', '19']}, columns = ['Name', 'Age'])
print("Created dataframe: \n{}".format(df))
Created dataframe: Name Age 0 Joyce 19 1 Joy 18 2 Ram 20 3 Maria 19
# Saving to a csv file
df.to_csv('students.csv')
# Saving to an excel file with sheet name
df.to_excel('students.xlsx', sheet_name='computer class')
Related Resources:
- Pandas Dataframe Slicing | Select specific entries Dataframe Slicing Pandas Datafrane slicing is one of the most important operations. Here’s how to do slicing in a pandas...
- Create Dataframe from Dictionary Create dataframe from dictionary Use “DataFrame” method of pandas library to create dataframe from dictionary # Imports import pandas as...
- Rename columns in Pandas | Change column Names Rename Columns in Pandas Set df.columns = List of column name strings to rename columns # Imports import pandas as...
- 8 Ways to Drop Columns in Pandas | A Detailed Guide 8 Ways to Drop Columns in Pandas Often there is a need to modify a pandas dataframe to remove unnecessary...
- Loop through rows in pandas | Iterate over rows Loop through rows in Pandas Use “iterrows” method of pandas dataframe to loop through rows # Imports import pandas as...
- Save Numpy array to csv file Save Numpy array to csv file Most machine learning engineers and Data Scientists use Numpy array because it’s efficient and...
- Delete rows based on column value Delete rows based on column value Use pandas dataframe’s inbuilt filter to delete rows based on column value # Imports...
- Read csv file to numpy array Read csv file to numpy array Use “savetxt” method of numpy to save a numpy array to csv file Use...
- Save Machine Learning model to a file | Pickle Save model to file Save machine learning model so that it can be used again and again without having to...
- Reset Index in Pandas Dataframe | Pandas tutorial Reset Index Reset index in pandas using “reset_index” method of pandas dataframe. When we perform slicing or filtering operations on...