Friday, 3 November 2023

Pandas: Add new row to the existing DataFrame

In this post, I am going to show how to add new rows to the DataFrame.

Approach 1: Using loc[label].

# Create a sample DataFrame
data = {'Name': ['Krishna', 'Ram', 'Joel', 'Gopi', 'Jitendra', "Raj"],
        'Age': [34, 25, 29, 41, 52, 23],
        'City': ['Bangalore', 'Chennai', 'Hyderabad', 'Hyderabad', 'Bangalore', 'Chennai']}

df = pd.DataFrame(data, index=[1, 2, 3, 4, 5, 6])

 

Above snippet generate below data set.

       Name  Age       City
1   Krishna   34  Bangalore
2       Ram   25    Chennai
3      Joel   29  Hyderabad
4      Gopi   41  Hyderabad
5  Jitendra   52  Bangalore
6       Raj   23    Chennai

 

As you see the output, I am using integers 1, 2… as index column. Below snippet add new row at the end of DataFrame.

 

df.loc[7] = ['Chaitu', 42, 'Nellore']

 

Above snippet add a new row at the end of the DataFrame. If you are not sure about the total rows in the DataFrame, you can use len() method to add new row.

 

df.loc[len(df)+1] = ['Ravi', 38, 'Vijayawada']

 

Find the below working application.

 

add_rows_using_loc.py

import pandas as pd

# Create a sample DataFrame
data = {'Name': ['Krishna', 'Ram', 'Joel', 'Gopi', 'Jitendra', "Raj"],
        'Age': [34, 25, 29, 41, 52, 23],
        'City': ['Bangalore', 'Chennai', 'Hyderabad', 'Hyderabad', 'Bangalore', 'Chennai']}

df = pd.DataFrame(data, index=[1, 2, 3, 4, 5, 6])
print(df)

print('\nAdd new person details using loc\n')
df.loc[7] = ['Chaitu', 42, 'Nellore']
df.loc[len(df)+1] = ['Ravi', 38, 'Vijayawada']
print(df)

 

Output

       Name  Age       City
1   Krishna   34  Bangalore
2       Ram   25    Chennai
3      Joel   29  Hyderabad
4      Gopi   41  Hyderabad
5  Jitendra   52  Bangalore
6       Raj   23    Chennai

Add new person details using loc

       Name  Age        City
1   Krishna   34   Bangalore
2       Ram   25     Chennai
3      Joel   29   Hyderabad
4      Gopi   41   Hyderabad
5  Jitendra   52   Bangalore
6       Raj   23     Chennai
7      Ravi   38  Vijayawada

 

Approach 2: Add new rows using concat method.

Following snippet concat the rows to a DataFrame.

rows = []
new_row1 = {'Name': 'Ramya', 'Age': 28, 'City': 'Bangalore', 'Gender': 'Female'}
new_row2 = {'Name': 'Sai', 'Age': 38, 'City': 'Bangalore', 'Gender': 'Male'}
rows.append(new_row1)
rows.append(new_row2)
df = pd.concat([df, pd.DataFrame(rows)], ignore_index=True)

 

add_row_using_concat.py

import pandas as pd
import numpy as np

# Create a sample DataFrame
data = {'Name': ['Krishna', 'Sailu', 'Joel', 'Chamu', 'Jitendra', "Raj"],
        'Age': [34, 35, 29, 41, 52, 31],
        'City': ['Bangalore', 'Hyderabad', 'Hyderabad', 'Chennai', 'Bangalore', 'Chennai'],
        'Gender': ['Male', 'Female', 'Male', 'Female', 'Male', 'Male']}
df = pd.DataFrame(data)
print(df)


# Add 2 rows
rows = []
new_row1 = {'Name': 'Ramya', 'Age': 28, 'City': 'Bangalore', 'Gender': 'Female'}
new_row2 = {'Name': 'Sai', 'Age': 38, 'City': 'Bangalore', 'Gender': 'Male'}
rows.append(new_row1)
rows.append(new_row2)

print('\nAdd 2 rows to the DataFrame\n')
df = pd.concat([df, pd.DataFrame(rows)], ignore_index=True)
print(df)

Output

       Name  Age       City  Gender
0   Krishna   34  Bangalore    Male
1     Sailu   35  Hyderabad  Female
2      Joel   29  Hyderabad    Male
3     Chamu   41    Chennai  Female
4  Jitendra   52  Bangalore    Male
5       Raj   31    Chennai    Male

Add 2 rows to the DataFrame

       Name  Age       City  Gender
0   Krishna   34  Bangalore    Male
1     Sailu   35  Hyderabad  Female
2      Joel   29  Hyderabad    Male
3     Chamu   41    Chennai  Female
4  Jitendra   52  Bangalore    Male
5       Raj   31    Chennai    Male
6     Ramya   28  Bangalore  Female
7       Sai   38  Bangalore    Male

 

 

 

 

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment