Saving a Pandas DataFrame as a CSV allows us to export processed data into a structured file format for storage and sharing. It helps preserve analysis results so the data can be reused across different tools and workflows. Here, we are taking sample data to convert DataFrame to CSV.
import pandas as pd
nme = ["Aparna", "Pankaj", "Sudhir", "Geeku"]
deg = ["MBA", "BCA", "M.Tech", "MBA"]
scr = [90, 40, 80, 98]
data = {'Name': nme, 'Degree': deg, 'Score': scr}
df = pd.DataFrame(data)
df
Output:

Export CSV to a Working Directory
Here, we simply export a Dataframe to a CSV file using df.to_csv().
df.to_csv('file1.csv')
Saving CSV Without Headers and Index
Here, we are saving the file with no header and no index number.
df.to_csv("file2.csv", header=False, index=False)
df = pd.read_csv("file2.csv", header=None)
df
Output:

Write a DataFrame to CSV file using Tab Separator
We can also save our file with some specific separate as we want. i.e "\t" .
import pandas as pd
users = {
"Name": ["Amit", "Cody", "Drew"],
"Age": [20, 21, 25]
}
df = pd.DataFrame(users)
df.to_csv("Users.csv", sep="\t", index=False)
new_df = pd.read_csv("Users.csv", sep="\t")
new_df
Output:
