This article will discuss how to replace the header with the first row in Pandas DataFrame.
A DataFrame is a data structure that stores the data in rows and columns. We can create a DataFrame using pandas.DataFrame() method. The header in a Dataframe refers to the column names.
We have to follow two steps to replace the header with the first row. That means we assign first row values as the column names in the DataFrame.
Step -1: Assign the first row data to the dataframe column attribute
We can do this by using iloc[] function. The row 1 index position is 0. So we are going to get the position using iloc[] and assign to the header (column) using columns attribute.
Syntax is as follows
dataframe.columns=dataframe.iloc[0]
where,
Frequently Asked:
- Select Rows where Two Columns are not equal in Pandas
- Check if all values in column are NaN in Pandas
- Create an empty DataFrame with just column names
- Get Column Index from Column Name in Pandas DataFrame
- dataframe is the input dataframe
- columns is used assign th columns
- iloc[0] is used to get the first row
Step -2: Get the data from second row and assign to the dataframe
We can do this by using slice operator. The row 2 index position is 1. so we are going to get the the data from row 2 onwards and assign to the dataframe
Syntax is as follows
dataframe=dataframe[1:]
where,
- dataframe is the input dataframe
- [1:] will get rows from the second row.
Let’s understand this by a working example. First we will create a dataframe,
#import pandas module import pandas as pd #create the dataframe with 4 columns data=pd.DataFrame({'id':[7058,7069,7060,7061], 'name':['sravan','bobby','ojaswi','deepu'], 'age':[21,23,22,21], 'subjects':['linux','html/css','node-js','php-mysql']}) #display print(data)
Output:
id name age subjects 0 7058 sravan 21 linux 1 7069 bobby 23 html/css 2 7060 ojaswi 22 node-js 3 7061 deepu 21 php-mysql
We created the dataframe with 4 rows and 4 columns
Example : Change header with first row in Pandas Dataframe
#set the location of the first row with columns data.columns = data.iloc[0] #remove first row from the dataframe rows data = data[1:] #display print(data)
Output:
7058 sravan 21 linux 1 7069 bobby 23 html/css 2 7060 ojaswi 22 node-js 3 7061 deepu 21 php-mysql
Here the first row is 7058 details , so it will become the header.
Replace first two rows as header in Pandas Dataframe
Here we have to specify the first and second row index positions in iloc[] function and get the rows from third index.
#set the location of the first row #and second row with columns data.columns = [data.iloc[0], data.iloc[1]] data.columns.names = ['', ''] #get from third row from the dataframe rows data = data[2:] #display print(data)
Output:
7069 bobby 23 html/css 7060 ojaswi 22 node-js 3 7061 deepu 21 php-mysql
Replace first row as header with reset_index()
We have to specify the row index position in iloc[] function as header. After that slice the data from 1st index location and reset the index using the reset_index() function. This function is used to reset the index again from 0.
Syntax is as follow:
dataframe.iloc[1:].reset_index(drop=True)
where,
- dataframe is the input dataframe.
- drop specifies to drop the index.
Lets create the dataframe
#import pandas module import pandas as pd #create the dataframe with 4 columns data=pd.DataFrame({'id':[7058,7069,7060,7061], 'name':['sravan','bobby','ojaswi','deepu'], 'age':[21,23,22,21], 'subjects':['linux','html/css','node-js','php-mysql']}) #display print(data)
Output:
id name age subjects 0 7058 sravan 21 linux 1 7069 bobby 23 html/css 2 7060 ojaswi 22 node-js 3 7061 deepu 21 php-mysql
Make first row as header using reset_index() method.
#set the location of the first row data.columns = data.iloc[0] #reset the index data = data.iloc[1:].reset_index(drop=True) data.columns.name = '' #display print(data)
Output:
7058 sravan 21 linux 0 7069 bobby 23 html/css 1 7060 ojaswi 22 node-js 2 7061 deepu 21 php-mysql
The Complete working example is as follows,
#import pandas module import pandas as pd #create the dataframe with 4 columns data=pd.DataFrame({'id':[7058,7069,7060,7061], 'name':['sravan','bobby','ojaswi','deepu'], 'age':[21,23,22,21], 'subjects':['linux','html/css','node-js','php-mysql']}) #display print(data) print('****** Example 1 **********') #set the location of the first row with columns data.columns = data.iloc[0] #remove first row from the dataframe rows data = data[1:] #display print(data) print('****** Example 2 **********') #set the location of the first row #and second row with columns data.columns = [data.iloc[0], data.iloc[1]] data.columns.names = ['', ''] #get from third row from the dataframe rows data = data[2:] #display print(data) print('****** Example 3 **********') #import pandas module import pandas as pd #create the dataframe with 4 columns data=pd.DataFrame({'id':[7058,7069,7060,7061], 'name':['sravan','bobby','ojaswi','deepu'], 'age':[21,23,22,21], 'subjects':['linux','html/css','node-js','php-mysql']}) #display print(data) print('****** Example 4 **********') #set the location of the first row data.columns = data.iloc[0] #reset the index data = data.iloc[1:].reset_index(drop=True) data.columns.name = '' #display print(data)
Output:
id name age subjects 0 7058 sravan 21 linux 1 7069 bobby 23 html/css 2 7060 ojaswi 22 node-js 3 7061 deepu 21 php-mysql ****** Example 1 ********** 0 7058 sravan 21 linux 1 7069 bobby 23 html/css 2 7060 ojaswi 22 node-js 3 7061 deepu 21 php-mysql ****** Example 2 ********** 7069 bobby 23 html/css 7060 ojaswi 22 node-js 3 7061 deepu 21 php-mysql ****** Example 3 ********** id name age subjects 0 7058 sravan 21 linux 1 7069 bobby 23 html/css 2 7060 ojaswi 22 node-js 3 7061 deepu 21 php-mysql ****** Example 4 ********** 7058 sravan 21 linux 0 7069 bobby 23 html/css 1 7060 ojaswi 22 node-js 2 7061 deepu 21 php-mysql
Summary
In this article, we discussed how to replace Header with First Row using four methods with examples.