Guides

How to add a row at the top in Pandas dataframe

March 22, 2021

March 22, 2021

Pandas is a great Python library for data analytics as it makes it relatively simple, but in some cases very simple data modifications that are super simple to make in Excel can be not so straightforward in Pandas. For example, to add a row at the top in Pandas dataframe you need to run multiple commands. Here is a detailed explanation of how you can add a row at the top in Pandas dataframe.

What we want to achieve


Let's say that below is our dataframe to which we want to add a row at the top (at index  0):

Our original df 

We want to insert a new row at the first position with the following data - name: Alex, age: 45, sex: male. We want to get the following dataframe:

What we want to have in df

Solution 1: using list and pd.concat()


This can be done by gathering the new data into a list first and then use pd.concat([], ignore_index=True) to add it at the top (at index  0) of dataframe.

Please see exsample code below:

# importing pandas import pandas as pd # creating a simple dataframe df = pd.DataFrame({'name': ['Jon','Tom','Jane','Kate'], 'age': [35,20,28,55], 'sex':['male','male','female','female']}) data = [] # always inserting new rows at the first position - last row will be always on top data.insert(0, {'name': 'Alex', 'age': 45, 'sex': 'male'}) # Concatanating original dataframe with additional row of data from list pd.concat([pd.DataFrame(data), df], ignore_index=True) # does not save changes to the original dataframe # Saving modified df as df2 df2 = pd.concat([pd.DataFrame(data), df], ignore_index=True) df2

Code to add a row of data at the top of Pandas dataframe

list and pd.concat() example in Jupyter Notebook 

Solution 2: using loc, index, and sort_index(inplace=True)


Another way to solve this challenge is by assigning a row to a particular index, using loc to incert data with index -1 and then modify indexes by adding 1 to all row indexes, so row with index -1 becomes 0 and row that had index 0 changes to

Remember that ealier record for Alex was added to df2, hence why we are adding row for Dean to df2.

# remember that df was not modified by code above. Record for Alex was added to df2 # another way of adding a row to the top of Pandas dataframe df2.loc[-1] = ['Dean', '25', 'male'] # adding a row df2.index = df2.index + 1 # shifting index df2.sort_index(inplace=True)

.loc .index and .sort_index(inplace=True) example in Jupyter Notebook 

Solution 3: using additional dataframe and pd.concat() with .reset_index(drop=True)


This solution is very similar to solution 1, we just replace list with additional dataframe than use pd.concat together with .reset_index(drop=True). Don't forget  to use .reset_index(drop=True) or you will end up with two rows set to index 0.

# adding row to dataframe at index 0 via additional dataframe and pd.concat df3 = pd.DataFrame([['Olivia',32,'female']], columns=['name','age','sex']) df4 = pd.concat([df3, df2]).reset_index(drop=True) df4

additional df and pd.concat() example in Jupyter Notebook

Subscribe

Get fresh web design stories, tips, and resources delivered straight to your inbox every week.

Get fresh web design stories, tips, and resources delivered straight to your inbox every week.

Continue Reading

Apps

Timestripe - my new favourite productivity app

March 5, 2023

Guides

How to scrape tables from websites using Pandas read_html() function

February 2, 2023

Guides

Drop all duplicate rows across multiple columns in Python Pandas

January 28, 2023

Guides

How to create effective prompts for AI image generation

August 15, 2022

Guides

Generate Huge Datasets With Fake Data Easily and Quickly using Python and Faker

April 16, 2022

Guides

How to change or update a specific cell in Python Pandas Dataframe

March 25, 2021

Guides

Creating WordClouds in Python from a single-column in Pandas dataframe

November 15, 2020

Guides

Python Regex examples - How to use Regex with Pandas

September 9, 2020

Guides

Python regular expressions (RegEx) simple yet complete guide for beginners

September 15, 2020

Guides

8 Python Pandas Value_counts() tricks that make your work more efficient

May 31, 2020

Guides

Exploring Correlation in Python: Pandas, SciPy

May 5, 2020

Guides

How to add new columns to Pandas dataframe?

March 22, 2020

Guides

Delete column/row from a Pandas dataframe using .drop() method

February 2, 2020

Guides

How to visualize data with Matplotlib from a Pandas Dataframe

November 15, 2019

Guides

The ultimate beginners guide to Group by in Python Pandas

August 8, 2019

Guides

Guide to renaming columns with Python Pandas

July 2, 2019

Guides

How to suppress scientific notation in Pandas

July 12, 2019

Guides

The complete beginners guide to Pandas

June 29, 2019

Guides

Data project #1: Stockmarket analysis

June 29, 2019

Blue and red light digital wallpaper
Blue and red light digital wallpaper

Guides

Use Jupyter notebooks anywhere

June 10, 2019