How to Save a Pandas Dataframe as a CSV in Python

import pandas as pd

# Create a sample DataFrame
data = {
‘Name’: [‘Alice’, ‘Bob’, ‘Charlie’],
‘Age’: [25, 30, 35],
‘City’: [‘New York’, ‘Los Angeles’, ‘Chicago’]
}

df = pd.DataFrame(data)

# Save DataFrame to CSV
df.to_csv(‘output.csv’, index=False) # index=False excludes the index column from the CSV

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top