5 Easy Ways to Add Rows to a Pandas Dataframe

Top 5 Ways To Add Rows To Pandas DataFrame

In this Python tutorial, we are going to discuss the top five ways to add or insert one or multiple rows to the pandas DataFrame object. So, let’s get started with our discussion.


Methods to Add Rows to a Pandas Dataframe

Let’s first create a sample pandas DataFrame object to start with and then we will keep adding one or multiple rows to it using the following methods.

# Import pandas Python module
import pandas as pd

# Create a sample pandas DataFrame object
df = pd.DataFrame({'RegNo': [111, 112, 113, 114, 115],
                   'Name': ['Gautam', 'Tanya', 'Rashmi', 'Kirti', 'Ravi'],
                   'CGPA': [8.85, 9.03, 7.85, 8.85, 9.45],
                   'Dept': ['ECE', 'ICE', 'IT', 'CSE', 'CHE'],
                   'City': ['Jalandhar','Ranchi','Patna','Patiala','Rajgir']})

# Print the created pandas DataFrame
print('Sample pandas DataFrame:\n')
print(df)

Output:

Sample pandas DataFrame:

   RegNo    Name  CGPA Dept       City
0    111  Gautam  8.85  ECE  Jalandhar
1    112   Tanya  9.03  ICE     Ranchi
2    113  Rashmi  7.85   IT      Patna
3    114   Kirti  8.85  CSE    Patiala
4    115    Ravi  9.45  CHE     Rajgir

Method #1

Add a pandas Series object as a row to the existing pandas DataFrame object.

# Create a pandas Series object with all the column values passed as a Python list
s_row = pd.Series([116,'Sanjay',8.15,'ECE','Biharsharif'], index=df.columns)

# Append the above pandas Series object as a row to the existing pandas DataFrame
# Using the DataFrame.append() function
df = df.append(s_row,ignore_index=True)

# Print the modified pandas DataFrame object after addition of a row
print('Modified Sample pandas DataFrame:\n')
print(df)

Output:

Modified Sample pandas DataFrame:

   RegNo    Name  CGPA Dept         City
0    111  Gautam  8.85  ECE    Jalandhar
1    112   Tanya  9.03  ICE       Ranchi
2    113  Rashmi  7.85   IT        Patna
3    114   Kirti  8.85  CSE      Patiala
4    115    Ravi  9.45  CHE       Rajgir
5    116  Sanjay  8.15  ECE  Biharsharif

Method #2

Add a Python dictionary as a row to the existing pandas DataFrame object.

# Create a Python dictionary object with all the column values
d_row = {'RegNo':117,'Name':"Sarthak",'CGPA':8.88,'Dept':"ECE",'City':"Allahabad"}

# Append the above Python dictionary object as a row to the existing pandas DataFrame
# Using the DataFrame.append() function
df = df.append(d_row,ignore_index=True)

# Print the modified pandas DataFrame object after addition of a row
print('Modified Sample pandas DataFrame:\n')
print(df)

Output:

Modified Sample pandas DataFrame:

   RegNo     Name  CGPA Dept         City
0    111   Gautam  8.85  ECE    Jalandhar
1    112    Tanya  9.03  ICE       Ranchi
2    113   Rashmi  7.85   IT        Patna
3    114    Kirti  8.85  CSE      Patiala
4    115     Ravi  9.45  CHE       Rajgir
5    116   Sanjay  8.15  ECE  Biharsharif
6    117  Sarthak  8.88  ECE    Allahabad

NOTE: Please set the ignore_index parameter of the DataFrame.append() function to True while passing a Python dictionary or a pandas Series otherwise, it will throw an error.

Method #3

Add a Python list object as a row to the existing pandas DataFrame object using DataFrame.loc[] method.

# Create a Python list object with all the column values
l_row = [118,"Kanika",7.88,"EE","Varanasi"]

# Append the above Python list object as a row to the existing pandas DataFrame
# Using the DataFrame.loc[]
df.loc[7] = l_row

# Print the modified pandas DataFrame object after addition of a row
print('Modified Sample pandas DataFrame:\n')
print(df)

Output:

Modified Sample pandas DataFrame:

   RegNo     Name  CGPA Dept         City
0    111   Gautam  8.85  ECE    Jalandhar
1    112    Tanya  9.03  ICE       Ranchi
2    113   Rashmi  7.85   IT        Patna
3    114    Kirti  8.85  CSE      Patiala
4    115     Ravi  9.45  CHE       Rajgir
5    116   Sanjay  8.15  ECE  Biharsharif
6    117  Sarthak  8.88  ECE    Allahabad
7    118   Kanika  7.88   EE     Varanasi

Method #4

Add the rows of one pandas DataFrame object to another pandas DataFrame object using the DataFrame.append() function.

# Create a new pandas DataFrame object
df2 = pd.DataFrame({'RegNo': [119, 120, 121],
                   'Name': ['Gaurav', 'Thaman', 'Radha'],
                   'CGPA': [8.85, 9.03, 7.85],
                   'Dept': ['ECE', 'ICE', 'IT'],
                   'City': ['Jalandhar','Ranchi','Patna']})

# Print the newly created pandas DataFrame object
print('New pandas DataFrame:\n')
print(df2)

# Append the rows of the above pandas DataFrame to the existing pandas DataFrame
# Using the DataFrame.append()
df = df.append(df2,ignore_index=True)

# Print the modified pandas DataFrame object after addition of rows
print('\nModified Sample pandas DataFrame:\n')
print(df)

Output:

New pandas DataFrame:

   RegNo    Name  CGPA Dept       City
0    119  Gaurav  8.85  ECE  Jalandhar
1    120  Thaman  9.03  ICE     Ranchi
2    121   Radha  7.85   IT      Patna

Modified Sample pandas DataFrame:

    RegNo    Name  CGPA Dept         City
0     111  Gautam  8.85  ECE    Jalandhar
1     112   Tanya  9.03  ICE       Ranchi
2     113  Rashmi  7.85   IT        Patna
3     114   Kirti  8.85  CSE      Patiala
4     115    Ravi  9.45  CHE       Rajgir
5     116  Sanjay  8.15  ECE  Biharsharif
6     116  Sanjay  8.15  ECE  Biharsharif
7     118  Kanika  7.88   EE     Varanasi
8     119  Gaurav  8.85  ECE    Jalandhar
9     120  Thaman  9.03  ICE       Ranchi
10    121   Radha  7.85   IT        Patna

Method #5

Add a row to the existing pandas DataFrame object at a specific index position using DataFrame.iloc[] method.

# Create a Python list object with all the column values
i_row = [122,"Zahir",6.88,"ME","Kolkata"]

# Append the above Python list object as a row to the existing pandas DataFrame
# At index 2 using the DataFrame.iloc[]
df.iloc[2] = i_row

# Print the modified pandas DataFrame object after addition of a row
print('Modified Sample pandas DataFrame:\n')
print(df)

Output:

Modified Sample pandas DataFrame:

    RegNo    Name  CGPA Dept         City
0     111  Gautam  8.85  ECE    Jalandhar
1     112   Tanya  9.03  ICE       Ranchi
2     122   Zahir  6.88   ME      Kolkata
3     114   Kirti  8.85  CSE      Patiala
4     115    Ravi  9.45  CHE       Rajgir
5     116  Sanjay  8.15  ECE  Biharsharif
6     116  Sanjay  8.15  ECE  Biharsharif
7     118  Kanika  7.88   EE     Varanasi
8     119  Gaurav  8.85  ECE    Jalandhar
9     120  Thaman  9.03  ICE       Ranchi
10    121   Radha  7.85   IT        Patna

NOTE: Kindly take care while using the DataFrame.iloc[] method, as it replaces the existing row at that index position with the new row.

Conclusion

In this tutorial, We have learned the top five methods to add or insert one or multiple rows to an existing pandas DataFrame object. Hope you have understood the things discussed above well and are ready to use these methods in your own data analysis project. Thanks for reading! Stay tuned with us for more exciting learning resources on Python programming.