How to save Pandas DataFrame

In this article, you’ll learn how to save the Pandas DataFrame. You can save the Pandas DataFrame in different formats. I will show you to save the DataFrame in four different formats. If you want to learn Pandas, I will recommend you two things book and free resource.

Save Pandas DataFrame to CSV

df.to_csv('filename.csv', index=False)

Example:

# Import the Pandas library as pd
import pandas as pd

# Initialize a dictionary
dict = {'Names':['Messi', 'John', 'Harry'],
        'Marks':[95, 90, 93]}

# Convert Dictionary into a DataFrame
df = pd.DataFrame(dict)

# Save Pandas DataFrame into CSV File 
df.to_csv('Marks.csv', index=False)

Save Pandas DataFrame to Excel File

df.to_excel("Marks.xlsx", index=False)

Save to JSON File

df.to_json('Marks.json', orient='index')

Save to Text File

df.to_csv('Marks.txt', header=None, index=None, sep='\t')

Leave a Comment

Your email address will not be published.