CSV files stores data in a comma-separated format.
You can export a pandas dataframe to CSV without the header and index using the header=None and index=False parameters in the to_csv() method.
Syntax
df.to_csv("User_details.csv", header=False, index=False)
This tutorial teaches you how to export a pandas dataframe to a CSV file without the header and the index.
Creating Dataframe
Create a sample dataframe that can be exported to a CSV file using the following code.
import pandas as pd
users = [ (1,'Shivam', 'Pandey', 'India', 1),
(2, 'Kumar', 'Ram' , 'India', 1 ),
(3, 'Felix','John' , 'Germany', 2 )
]
#Create a DataFrame object
df = pd.DataFrame(users,
columns = ['RegNo.','First Name', 'Last Name', 'Country', 'Country_Code']
)
df
DataFrame Will Look Like
RegNo. | First Name | Last Name | Country | Country_Code | |
---|---|---|---|---|---|
0 | 1 | Shivam | Pandey | India | 1 |
1 | 2 | Kumar | Ram | India | 1 |
2 | 3 | Felix | John | Germany | 2 |
Exporting Without Header
The to_csv() method is used to convert the pandas dataframe into a CSV file. It also accepts other optional parameters to configure headers, indexes, and other properties.
To export the pandas dataframe without a header,
- Use the
header=False
parameter. - This parameter is optional, and by default, the
to_csv()
method uses the pandas dataframe header as a header for the exported CSV file.
Code
df.to_csv("User_details.csv", header=False)
CSV File Will Look Like
The CSV file is exported without a header, which looks like the following.
0,1,Shivam,Pandey,India,1
1,2,Kumar,Ram,India,1
2,3,Felix,John,Germany,2
Exporting Without Index Column
You can use the index
parameter to export the pandas dataframe to a CSV file without an index column(Remove column headers).
- Use the
index=False
parameter to avoid index while exporting the dataframe to a CSV file - The pandas dataframe always has an
index
. It is either a user-defined column or a sequence of numbers. By default, this column will be exported to the CSV file if the index parameter is not specified in theto_csv()
method. Code
df.to_csv("User_details.csv", index=False)
CSV File Will Look Like
RegNo.,First Name,Last Name,Country,Country_Code
1,Shivam,Pandey,India,1
2,Kumar,Ram,India,1
3,Felix,John,Germany,2
Exporting Without Header and Index
To export the pandas dataframe without header and index columns, use the header
and index
parameters in the to_csv()
method.
- Use
header=False
to remove column headers while exporting pandas dataframe to CSV file - Use
index=Flase
to remove index columns
Code
df.to_csv("User_details.csv", header=False, index=False)
CSV File Will Look Like
1,Shivam,Pandey,India,1
2,Kumar,Ram,India,1
3,Felix,John,Germany,2
Set Column names While Exporting
To set the custom names for the column header while exporting to a CSV file, you use the header
parameter and pass the list of column names.
- Pass the list of column names using the
header
parameter. - The number of items in the list must be equal to the number of columns in the pandas dataframe
Code
df.to_csv("User_details.csv", header = ['Serial_Number','Name' , 'Last Name', 'Nation', 'Nation_Code'])
CSV File Will Look Like
,Serial_Number,Name,Last Name,Nation,Nation_Code
0,1,Shivam,Pandey,India,1
1,2,Kumar,Ram,India,1
2,3,Felix,John,Germany,2