In this post, we will learn, How to add a row at top of DataFrame Pandas with examples.The Pandas dataframe Appen(),loc[] and concat() method is used. To use the Pandas library first we have to install it on the local system by using the pip command “pip install pandas” and import it into our code by using “import pandas as pd” to use its functions.
1. How to add a row at top of DataFrame Pandas using concat
The pandas concat() method “performs concatenation operations along an axis while performing optional set logic (union or intersection). In this example, we have data frames df_stud and row_to_add that we are concatenating by using the contact() method and reset_index() is used to reset the index after adding a row at top of the existing dataframe.
import pandas as pd
mydict = {'Name':['Trex','Jack','Rack'],'Subj':['comp','Math','Phy'],
'Marks':[100,100,98]}
df_stud = pd.DataFrame(mydict)
print('Original Dataframe:\n',df_stud)
row_to_add = pd.DataFrame({'Name':'Tom',
'Marks':100,
'Subj':'Phy'
},index =[0])
df_stud = pd.concat([row_to_add, df_stud]).reset_index(drop = True)
print(f'\nDatafram after append :\n{df_stud}')
Output
Original Dataframe:
Name Subj Marks
0 Trex comp 100
1 Jack Math 100
2 Rack Phy 98
Datafram after append :
Name Marks Subj
0 Tom 100 Phy
1 Trex 100 comp
2 Jack 100 Math
3 Rack 98 Phy
2. How to add a row at top of DataFrame Pandas using concat and loc[]
In this below example, we have two DataFrames df_stud and row_to_add that we are concatenating by using the contact() method along with loc[] that Access a group of rows and columns by label(s) or a boolean array. The reset_index() is used to reset the index after adding a row at top of the existing dataframe.
import pandas as pd
mydict = {'Name':['Trex','Jack','Rack'],'Subj':['comp','Math','Phy'],
'Marks':[100,100,98]}
df_stud = pd.DataFrame(mydict)
print('Original Dataframe:\n',df_stud)
row_to_add = pd.DataFrame({'Name':'Tom',
'Marks':100,
'Subj':'Phy'
},index =[0])
df_stud = pd.concat([row_to_add, df_stud.loc[:]]).reset_index(drop = True)
print(f'\nDatafram after append :\n{df_stud}')
Output
Original Dataframe:
Name Subj Marks
0 Trex comp 100
1 Jack Math 100
2 Rack Phy 98
Datafram after append :
Name Marks Subj
0 Tom 100 Phy
1 Trex 100 comp
2 Jack 100 Math
3 Rack 98 Phy
3. How to add a row at top of DataFrame Pandas using concat
In this example, we have added a row at top of the Panda dataframe by using the contact()m method and df[:].The reset_index() is used to Reset the index or a level of it. We are resetting the index after concatenating the row at top of the dataframe.
- Import the Pandas library using “import pandas as pd”
- Create a dataframe df_stud from the dictionary
- Create a new row as dataframe row_to_add
- Call the pd.concat() method to concatenate and reset_index() is used to reset indexes of the dataframe after adding a row.
import pandas as pd
mydict = {'Name':['Trex','Jack','Rack'],'Subj':['comp','Math','Phy'],
'Marks':[100,100,98]}
df_stud = pd.DataFrame(mydict)
print('Original Dataframe:\n',df_stud)
row_to_add = pd.DataFrame({'Name':'Tom',
'Marks':100,
'Subj':'Phy'
},index =[0])
df_stud = pd.concat([row_to_add, df_stud[:]]).reset_index(drop = True)
print(f'\nDatafram after append :\n{df_stud}')
Output
Original Dataframe:
Name Subj Marks
0 Trex comp 100
1 Jack Math 100
2 Rack Phy 98
Datafram after append :
Name Marks Subj
0 Tom 100 Phy
1 Trex 100 comp
2 Jack 100 Math
3 Rack 98 Phy