Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add a row at top in pandas dataframe [duplicate]

Tags:

python

pandas

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?

like image 947
Shubham R Avatar asked Apr 14 '17 09:04

Shubham R


People also ask

How do I add a row to the top of a pandas DataFrame?

Use concat() to Add a Row at Top of DataFrame Use pd. concat([new_row,df. loc[:]]). reset_index(drop=True) to add the row to the first position of the DataFrame as Index starts from zero.

How do I add a row to a data frame?

You can add rows to the pandas dataframe using df. iLOC[i] = ['col-1-value', 'col-2-value', ' col-3-value '] statement. Other options available to add rows to the dataframe are, append()

How do I append multiple rows to a DataFrame in Python?

Add multiple rows to pandas dataframe We can pass a list of series too in the dataframe. append() for appending multiple rows in dataframe. For example, we can create a list of series with same column names as dataframe i.e. Now pass this list of series to the append() function i.e.


6 Answers

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
like image 109
edyvedy13 Avatar answered Oct 19 '22 16:10

edyvedy13


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...

like image 32
MaxU - stop WAR against UA Avatar answered Oct 19 '22 14:10

MaxU - stop WAR against UA


@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
like image 40
Edward Atkins Avatar answered Oct 19 '22 15:10

Edward Atkins


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
like image 34
Serenity Avatar answered Oct 19 '22 14:10

Serenity


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

like image 22
dylan_fan Avatar answered Oct 19 '22 16:10

dylan_fan


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
like image 29
husimu Avatar answered Oct 19 '22 16:10

husimu