Dataframe column headers are used to identify columns.
You can remove the header row from the Pandas dataframe using the df.columns = range(df.shape[1]) statement.
This tutorial teaches you the different methods to remove the header row from Pandas dataframe and when it is appropriate to use each method.
Table of Contents
Sample Dataframe
Create a sample dataframe with headers.
This dataframe will be used to remove headers using different methods.
import pandas as pd
users = [('Shivam', 'Pandey', 'India'),
('Kumar', 'Ram' , 'India'),
('Felix','John' , 'Germany')
]
#Create a DataFrame object
df = pd.DataFrame(users,
columns = ['First Name' , 'Last Name', 'Country']
)
df
DataFrame Will Look Like
First Name | Last Name | Country | |
---|---|---|---|
0 | Shivam | Pandey | India |
1 | Kumar | Ram | India |
2 | Felix | John | Germany |
Using Range And Shape
The range() function returns a sequence of numbers in a given range.
To remove the column headers and set the column indexes as the header,
- Pass the range of columns using the
df.shape[1]
to the range function, and it’ll return the range of numbers. - Assign the value to the
df.columns
attribute.
Code
The following code demonstrates how to use the range function to remove the header and set the column indexes as header.
df.columns = range(df.shape[1])
df
DataFrame Will Look Like
0 | 1 | 2 | 3 | 4 | 5 | |
---|---|---|---|---|---|---|
0 | John | Doe | 120 jefferson st. | Riverside | NJ | 8075 |
1 | Jack | McGinnis | 220 hobo Av. | Phila | PA | 9119 |
2 | John “Da Man” | Repici | 120 Jefferson St. | Riverside | NJ | 8075 |
3 | Stephen | Tyler | 7452 Terrace “At the Plaza” road | SomeTown | SD | 91234 |
4 | NaN | Blankman | NaN | SomeTown | SD | 298 |
5 | Joan “the bone”, Anne | Jet | 9th, at Terrace plc | Desert City | CO | 123 |
Using Dictionary
This section teaches you how to completely remove the header information from the pandas dataframe using a dictionary.
- Create a dictionary with keys as existing columns and values as empty strings
- Rename the dataframe using the columns attribute and pass the dictionary, which has the empty string mappings for each column.
- Use
inplace=True
to perform the replace operation in the same dataframe instead of creating a new copy.
Code
The following code demonstrates how to use the dictionary to remove header information from the Pandas dataframe.
col_dict = dict.fromkeys(df.columns, '')
df.rename(columns = col_dict, inplace=True)
df
DataFrame Will Look Like
0 | Shivam | Pandey | India |
---|---|---|---|
1 | Kumar | Ram | India |
2 | Felix | John | Germany |
Remove Header While Reading CSV
To remove header information while reading a CSV file and creating a pandas dataframe, you can use th header=None
parameter in the read_csv()
method.
- By default, the
read_csv()
method considers the first row of the CSV file as the header. - While using
header=None
, the first row will not be considered a header, and the column indexes will be used as a header.
Code
import pandas as pd
df = pd.read_csv('addresses.csv', header=None)
df
DataFrame Will Look Like
0 | 1 | 2 | 3 | 4 | 5 | |
---|---|---|---|---|---|---|
0 | John | Doe | 120 jefferson st. | Riverside | NJ | 8075 |
1 | Jack | McGinnis | 220 hobo Av. | Phila | PA | 9119 |
2 | John “Da Man” | Repici | 120 Jefferson St. | Riverside | NJ | 8075 |
3 | Stephen | Tyler | 7452 Terrace “At the Plaza” road | SomeTown | SD | 91234 |
4 | NaN | Blankman | NaN | SomeTown | SD | 298 |
5 | Joan “the bone”, Anne | Jet | 9th, at Terrace plc | Desert City | CO | 123 |