5

Replace Header With First Row In Pandas Dataframe

 2 years ago
source link: https://thispointer.com/replace-header-with-first-row-in-pandas-dataframe/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Replace Header With First Row In Pandas Dataframe – thisPointer

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.

Advertisements

vid5e62792b95ec8618094391.jpg?cbuster=1600267117
00:00/15:21
10 Sec
Lambda Functions in Python
liveView.php?hash=ozcmPTEznXRiPTEzqzyxX2V2ZW50PTUjJaNypaZypyRcoWU9MTYmOTx0MTY0NlZ2nWRspGkurWVlVzVlPTMhMS4jJaM9MTAkMwx3JaN0YT0jJat9NDUmJax9MmI1JaZcZF9jYXNmRG9gYWyhPXRbnXNjo2yhqGVlLzNioSZmqWJJZD10nGympG9coaRypv5wo20zZGVvqWqJozZipz1uqGyiow0znXNBpHA9MCZlnT02QmY5NmY2NTUmNmQ2MTp0NmM3QmpmNxImMTqCNTQmMDqEN0I2NDMlMmAmMwMkMxQmMTMlMxQmMTM5NUYmMwMkN0Q3QwpmMmEmMwMmMmQmOTM2MmQmOTqEN0I0MmMkMmpmMwqEN0I1MmY0NDp2ODpjNwMmMmQlNmY2MTU3MmUmMDVBNTt0OTp1NTxmMwM5NmQ3RDqCNwI2MmY4NmI2RwZENwU3RDqCNmE2NDY1NmM2Qwp0NxY3MDqEN0I2RwZDNwx2RTp1Nmt3RDqCNTtmNDM1MmM3RDqCNTxmMmMlMmU3RDqCNwYmMTqEN0I0QmMkMmImNTMlMmE3REZFRxUzZGyunWQ9JaVmZXJJpEFxZHI9MTQkLwE2N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ElMCUlMFNuZzFlnSUlRwUmNl4mNvZwp3V1nWQ9NwFvZwt2MGYmMmx0YSZwo250ZW50RzyfZUyxPTAzoWVxnWFQoGF5TGymqEyxPTAzoWVxnWFMnXN0SWQ9MCZxqXI9ODAmJzqxpHI9MCZaZHBlQ29hp2VhqD0znXNXZVBup3NHZHBlPTEzY2NjYT0jJzNwpGFDo25mZW50PSZwYaVmqGVlPTE2Mmx5NDE2NTp4NwYzqWyxPVNyn2yhZG9TUGkurWVlNwFvZwt2MGZyMmZyNlZjqWJVpzj9nHR0pHMyM0EyMxYyMxZ0nGympG9coaRypv5wo20yMxZlZXBfYWNyLWuyYWRypv13nXRbLWZcpaN0LXJiql1cov1jYW5xYXMgZGF0YWZlYW1yJTJGJzZfo2F0U3RuqHVmPWZuoHNyJzVcZHNjPXBlZWJcZA==

Syntax is as follows

dataframe.columns=dataframe.iloc[0]
dataframe.columns=dataframe.iloc[0]

where,

  • 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:]
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)
#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
     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)
#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
   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)
#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
   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)
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)
#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
     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)
#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
   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)
#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
     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.

Advertisements


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK