CSVs or comma-separated values are extremely useful formats to sort the data in defined text files (usually separated by commas) and arrange the data in separate rows. They are frequently utilized in numerous applications due to their interoperability which enables you to easily move the data between multiple formats.
Pandas to_csv() exports your DataFrame as a comma-separated value (CSV) datatype to your machine. This means that you can return your data whenever you want. To utilize this method, we have to follow the following provided syntax:
Syntax:
label=None)
Parameters:
-
- The first parameter is the name of the CSV file.
- By default, the index is True. If we specify False, the row indices are removed in CSV.
- By default, the header is True. If we specify False, the columns are removed in CSV.
- If you want to convert only the particular columns of DataFrame to CSV, you need to pass those columns to the columns parameter in a list.
- The index_label takes an existing column and sets this column as an index in the CSV file.
Example 1: Passing Only the Paramater
Create a DataFrame with 5 columns and 5 records. Convert it into a CSV file by passing only the file_name as a parameter.
# Consider the DataFrame having 5 columns
program=pandas.DataFrame({'manager id':[1,2,3,4,5],
'name':['pill','dee dee','ghorak','teon','marky'],
'Idea':['House drainage','All','water supply','electricity','drilling'],
'demography':['ap','gujarat','patna','indore','norway']})
print(program)
# Export the above DataFrame to the csv file.
program.to_csv("Program.csv")
print('Exported to CSV..')
Output:
0 1 pill House drainage ap
1 2 dee dee All gujarat
2 3 ghorak water supply patna
3 4 teon electricity indore
4 5 marky drilling norway
Exported to CSV..
The file_name that we pass is “Program.csv”. When you open the file, you can see that the entire DataFrame is converted to a CSV file.
Example 2: Without the Index
Let’s convert the previous DataFrame to CSV by ignoring the index. To do this, set the index parameter to False.
# Consider the DataFrame having 5 columns
program=pandas.DataFrame({'manager id':[1,2,3,4,5],
'name':['pill','dee dee','ghorak','teon','marky'],
'Idea':['House drainage','All','water supply','electricity','drilling'],
'demography':['ap','gujarat','patna','indore','norway']})
# Export the above DataFrame to the csv file without index
program.to_csv("Program.csv",index=False)
print('Exported to CSV without Index..')
Output:
The file_name that we pass is “Program.csv”. When you open the file, you can see that the entire DataFrame is converted to a CSV file without indices.
Example 3: Without the Header
Let’s convert the previous DataFrame to CSV by ignoring the column. To do this, set the header parameter to False.
# Consider the DataFrame having 5 columns
program=pandas.DataFrame({'manager id':[1,2,3,4,5],
'name':['pill','dee dee','ghorak','teon','marky'],
'Idea':['House drainage','All','water supply','electricity','drilling'],
'demography':['ap','gujarat','patna','indore','norway']})
# Export the above DataFrame to the csv file without header
program.to_csv("Program.csv",header=False)
print('Exported to CSV without header..')
Output:
The file_name that we pass is “Program.csv”. When you open the file, you can see that the entire DataFrame is converted to a CSV file without column names.
Example 4: Particular Columns to CSV
So far, we have seen how to convert the entire DataFrame to CSV. Now, we convert only the particular columns in the existing DataFrame to a CSV file.
# Consider the DataFrame having 5 columns
program=pandas.DataFrame({'manager id':[1,2,3,4,5],
'name':['pill','dee dee','ghorak','teon','marky'],
'Idea':['House drainage','All','water supply','electricity','drilling'],
'demography':['ap','gujarat','patna','indore','norway']})
# Export only 2 column in the above DataFrame to the csv
program.to_csv("Program.csv",columns=['name','Idea'])
print('Exported to CSV..')
Output:
The file_name that we pass is “Program.csv”. When you open the file, you can see that only the “name” and “Idea” columns in the DataFrame are converted to a CSV file.
Example 5: With Index_Label
Set the “manager id” column as index in the converted CSV file by setting the “manager id” column to the index_label.
# Consider the DataFrame having 5 columns
program=pandas.DataFrame({'manager id':[1,2,3,4,5],
'name':['pill','dee dee','ghorak','teon','marky'],
'Idea':['House drainage','All','water supply','electricity','drilling'],
'demography':['ap','gujarat','patna','indore','norway']})
# Export above DataFrame to csv file by setting 'manager id' column as index.
program.to_csv("Program.csv",index_label='manager id')
print('Exported to CSV with index - manager id.')
Output:
You can see that the “manager id” column is set to index.
Conclusion
Exporting a Pandas DataFrame into a CSV file is a very useful practice. In this writing, we briefly informed you about the CSV files and explained the “to_csv()” Pandas method which is used to export the DataFrame to CSV files. The syntax with all the 5 parameters is explained in this guide with an example.