61

Below is my dataframe

import pandas as pd
df = pd.DataFrame({'name': ['jon','sam','jane','bob'],
           'age': [30,25,18,26],
           'sex':['male','male','female','male']})


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

I want to insert a new row at the first position

name: dean, age: 45, sex: male

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

What is the best way to do this in pandas?

4
  • @MaxU i edited my question
    – Shubham R
    Commented Apr 14, 2017 at 9:18
  • One more question - is it going to be a regular (frequent) operation or not? Commented Apr 14, 2017 at 9:25
  • 1
    @MaxU yes it would be a frequent operation
    – Shubham R
    Commented Apr 14, 2017 at 9:28
  • Incase someone looking for making columns as first row, df.T.reset_index().T Commented Jun 8, 2022 at 11:37

6 Answers 6

69

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
9
  • 2
    df.index + 1 - nice idea! Commented Apr 14, 2017 at 9:26
  • 1
    I would just add slightly cleaner last line: df.sort_index(inplace=True)
    – Rohit
    Commented Feb 13, 2018 at 21:43
  • 1
    You plagiarised this answer from stackoverflow.com/a/24284680/4909087
    – cs95
    Commented Jun 25, 2018 at 2:48
  • @coldspeed: look at dates of creation please Commented Jun 25, 2018 at 2:56
  • 1
    @Call Centre Executive, It was not my intention, mostly likely I saw the answer before, used it for my own code and then answered here when a similar question popped up.
    – edyvedy13
    Commented Jun 25, 2018 at 8:51
53

If it's going to be a frequent operation, then it makes sense (in terms of performance) to gather the data into a list first and then use pd.concat([], ignore_index=True) (similar to @Serenity's solution):

Demo:

data = []

# always inserting new rows at the first position - last row will be always on top    
data.insert(0, {'name': 'dean', 'age': 45, 'sex': 'male'})
data.insert(0, {'name': 'joe', 'age': 33, 'sex': 'male'})
#...

pd.concat([pd.DataFrame(data), df], ignore_index=True)

In [56]: pd.concat([pd.DataFrame(data), df], ignore_index=True)
Out[56]:
   age  name     sex
0   33   joe    male
1   45  dean    male
2   30   jon    male
3   25   sam    male
4   18  jane  female
5   26   bob    male

PS I wouldn't call .append(), pd.concat(), .sort_index() too frequently (for each single row) as it's pretty expensive. So the idea is to do it in chunks...

7

@edyvedy13's solution worked great for me. However it needs to be updated for the deprecation of pandas' sort method - now replaced with sort_index.

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

Use pandas.concat and reindex new dataframe:

import pandas as pd
df = pd.DataFrame({'name': ['jon','sam','jane','bob'],
           'age': [30,25,18,26],
           'sex':['male','male','female','male']})
# new line
line = pd.DataFrame({'name': 'dean', 'age': 45, 'sex': 'male'}, index=[0])
# concatenate two dataframe
df2 = pd.concat([line,df.ix[:]]).reset_index(drop=True)
print (df2)

Output:

   age  name     sex
0   45  dean    male
1   30   jon    male
2   25   sam    male
3   18  jane  female
4   26   bob    male
1
import pandas as pd


df = pd.DataFrame({'name': ['jon','sam','jane','bob'],
           'age': [30,25,18,26],
           'sex': ['male','male','female','male']})

df1 = pd.DataFrame({'name': ['dean'], 'age': [45], 'sex':['male']})
df1 = df1.append(df)
df1 = df1.reset_index(drop=True)

That works

1

This will work for me.

>>> import pandas as pd
>>> df = pd.DataFrame({'name': ['jon','sam','jane','bob'],
...            'age': [30,25,18,26],
...            'sex':['male','male','female','male']})     >>> df
   age  name     sex
0   30   jon    male
1   25   sam    male
2   18  jane  female
3   26   bob    male
>>> df.loc['a']=[45,'dean','male']
>>> df
   age  name     sex
0   30   jon    male
1   25   sam    male
2   18  jane  female
3   26   bob    male
a   45  dean    male
>>> newIndex=['a']+[ind for ind in df.index if ind!='a']
>>> df=df.reindex(index=newIndex)
>>> df
   age  name     sex
a   45  dean    male
0   30   jon    male
1   25   sam    male
2   18  jane  female
3   26   bob    male

Not the answer you're looking for? Browse other questions tagged or ask your own question.