Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
95 views
in Information Technology by (51.9k points)
closed by

Exporting a DataFrame to a CSV file.

1 Answer

+1 vote
by (49.5k points)
selected by
 
Best answer

We can use the to_csv() function to save a DataFrame to a text or csv file. For example, to save the DataFrame ResultDF created in the previous section; we can use the following statement

>>> ResultDF

>>> ResultDF.to_csv(path_or_buf='C:/images/ 

resultout.csv', sep=',')

This creates a file by the name resultout.csv in the folder C:/images on the hard disk. When we open this file in any text editor or a spreadsheet, we will find the above data along with the row labels and the column headers, separated by comma.

In case we do not want the column names to be saved to the file we may use the parameter header=False. Another parameter index=False is used when we do not want the row labels to be written to the file on disk. 

For example:

>>> ResultDF.to_csv( 'C:/images/resultonly.txt', 

sep = '@', header = False, index= False) 

If we open the file resultonly.txt, we will find the following contents:

90@92@89@81@94 

91@81@91@71@95 

97@96@88@67@99

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...