add a row at top in pandas dataframe [duplicate]

Probably this is not the most efficient way but:

df.loc[-1] = ['45', 'Dean', 'male']  # adding a row
df.index = df.index + 1  # shifting index
df.sort_index(inplace=True) 

Output:

 age  name     sex
0  45  Dean    male
1  30   jon    male
2  25   sam    male
3  18  jane  female
4  26   bob    male

Leave a Comment