Add Row to Top of Pandas DataFrame

In this tutorial, you will learn multiple ways to add rows to the top of a Pandas DataFrame.

We’ll use different methods such as loc[], concat(), reindex(). and insert().

 

 

Using loc[]

The loc[] property allows you to access a group of rows and columns by labels or a boolean array.

Let’s see how you can use loc[] to add a row to the top of a DataFrame.

First, you’ll need to create a sample DataFrame:

import pandas as pd
data = {'ID': [1, 2, 3],
        'Plan': ['Basic', 'Standard', 'Premium'],
        'Cost': [10, 20, 30]}
df = pd.DataFrame(data)
print(df)

Output:

   ID      Plan  Cost
0   1     Basic    10
1   2  Standard    20
2   3   Premium    30

Now, let’s add a row at the top of this DataFrame.

new_row = pd.Series([0, 'Free', 0], index=df.columns)
df.loc[-1] = new_row

# Sort the DataFrame to make the new row the first row
df.sort_index(inplace=True)
print(df)

Output:

   ID      Plan  Cost
-1   0      Free     0
 0   1     Basic    10
 1   2  Standard    20
 2   3   Premium    30

Here, a new row with index -1 is added to the DataFrame. This row is placed at the top when you sort the DataFrame index.

 

Using concat()

Another method to add a row to the top of a DataFrame is by using the concat() function.

This function provides various ways to combine DataFrames including horizontally, vertically, and along a particular axis.

First, let’s create a sample DataFrame similar to the one used earlier.

import pandas as pd
data = {'ID': [1, 2, 3],
        'Plan': ['Basic', 'Standard', 'Premium'],
        'Cost': [10, 20, 30]}
df = pd.DataFrame(data)
print(df)

Output:

   ID      Plan  Cost
0   1     Basic    10
1   2  Standard    20
2   3   Premium    30

Now, let’s prepare the new row and add it to the top of the DataFrame.

# Create a new DataFrame for the row to be added
new_row = pd.DataFrame({'ID': [0], 'Plan': ['Free'], 'Cost': [0]})

# Use concat() to add the new row at the top
df = pd.concat([new_row, df]).reset_index(drop=True)
print(df)

Output:

   ID      Plan  Cost
0   0      Free     0
1   1     Basic    10
2   2  Standard    20
3   3   Premium    30

Here, the concat() function combines the new DataFrame (new_row) and the original DataFrame (df).

The reset_index() part is crucial; it resets the index of the DataFrame.

 

Using df.reindex

By using reindexmethod, you can alter the row index and manipulate the DataFrame structure.

As usual, let’s create a sample DataFrame.

import pandas as pd
data = {'ID': [1, 2, 3],
        'Plan': ['Basic', 'Standard', 'Premium'],
        'Cost': [10, 20, 30]}
df = pd.DataFrame(data)
print(df)

Output:

   ID      Plan  Cost
0   1     Basic    10
1   2  Standard    20
2   3   Premium    30

The DataFrame comprises three rows and three columns: ‘ID’, ‘Plan’, and ‘Cost’.

Now, let’s use reindex to add a new row at the top.

# Create a new index list including the new index
new_index = [-1] + list(df.index)

# Reindex the DataFrame
df = df.reindex(new_index)

# Fill in the new row
df.loc[-1] = [0, 'Free', 0]

# Sort the DataFrame by index
df.sort_index(inplace=True)
print(df)

Output:

   ID      Plan  Cost
-1   0      Free     0
 0   1     Basic    10
 1   2  Standard    20
 2   3   Premium    30

In this example, you first create a new index list that includes the new index -1 along with the existing indices.

After using reindex, you get a DataFrame with an additional row that has all NaN values.

You then fill in the new row using the .loc[] property.

 

Using df.insert

The df.insert() method is used to add columns to a DataFrame, but you can get creative and use it to add a row at the top as well.

Here’s a sample DataFrame:

import pandas as pd
data = {'ID': [1, 2, 3],
        'Plan': ['Basic', 'Standard', 'Premium'],
        'Cost': [10, 20, 30]}
df = pd.DataFrame(data)
print(df)

Output:

   ID      Plan  Cost
0   1     Basic    10
1   2  Standard    20
2   3   Premium    30

Now, let’s transpose the DataFrame, insert a new column at the top, and then transpose it back.

# Transpose the DataFrame
df_T = df.T

# Insert a new column at index 0
new_column = [0, 'Free', 0]
df_T.insert(0, 'new_row', new_column)

# Transpose back to the original form
df = df_T.T.reset_index(drop=True)
print(df)

Output:

  ID      Plan Cost
0  0      Free    0
1  1     Basic   10
2  2  Standard   20
3  3   Premium   30

In this method, you first transpose the DataFrame using df.T, swapping rows with columns.

You then use df.insert() to add a new column, which will later become the top row once the DataFrame is transposed back.

Finally, reset_index(drop=True) is applied to clean up the index.

Leave a Reply

Your email address will not be published. Required fields are marked *