How to save Pandas DataFrame as Text File

You can save the Pandas DataFrame as a text file with the given code.

# Import the Pandas library as pd
import pandas as pd

# Initialize a dictionary
dict = {'Students':['Harry', 'John', 'Hussain', 'Satish'],
        'Scores':[77, 59, 88, 93]}

# Create a DataFrame
df = pd.DataFrame(dict)

# Display the Output

# save dataframe as text file
df.to_csv('Data.txt', header=None, index=None, sep='\t')

Free Learning Resources

Recommended Laptops

Leave a Comment

Your email address will not be published.