Related Articles

Related Articles

Add a row at top in pandas DataFrame
  • Last Updated : 18 Dec, 2018

Pandas DataFrame is two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns).

Let’s see how can we can add a row at top in pandas DataFrame.

Observe this dataset first.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas module 
import pandas as pd 
    
# making data frame 
  
df.head(10)

chevron_right


Code #1: Adding row at the top of given datarame by concatenating the old dataframe with new one.



filter_none

edit
close

play_arrow

link
brightness_4
code

new_row = pd.DataFrame({'Name':'Geeks', 'Team':'Boston', 'Number':3,
                        'Position':'PG', 'Age':33, 'Height':'6-2',
                        'Weight':189, 'College':'MIT', 'Salary':99999},
                                                            index =[0])
# simply concatenate both dataframes
df = pd.concat([new_row, df]).reset_index(drop = True)
df.head(5)

chevron_right


Output:

 
Code #2: Adding row at the top of given datarame by concatenating the old dataframe with new one.

filter_none

edit
close

play_arrow

link
brightness_4
code

new_row = pd.DataFrame({'Name':'Geeks', 'Team':'Boston', 'Number':3,
                        'Position':'PG', 'Age':33, 'Height':'6-2',
                        'Weight':189, 'College':'MIT', 'Salary':99999}, index =[0])
  
# Concatenate new_row with df 
df = pd.concat([new_row, df[:]]).reset_index(drop = True)
df.head(5)

chevron_right


Output:

 
Code #3: Adding row at the top of given datarame by concatenating the old dataframe with new one using df.ix[] method.

filter_none

edit
close

play_arrow

link
brightness_4
code

new_row = pd.DataFrame({'Name':'Geeks', 'Team':'Boston', 'Number':3,
                        'Position':'PG', 'Age':33, 'Height':'6-2',
                        'Weight':189, 'College':'MIT', 'Salary':99999}, index =[0])
  
df = pd.concat([new_row, df.ix[:]]).reset_index(drop = True)
df.head(5)

chevron_right


Output:

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :