How to add a row to a dataframe in python

How to add a row in dataframe in Python

In pandas, dataframe is a fundamenta data structure which provides tool for working with structures data. Adding a row falls under one of CRUD operation. The operation of adding a row to a dataframe in python should be known all the developers. In this blog we will explore how to add a row to a dataframe in python.

How to add a row to a dataframe in python

Before inserting a row in a dataframe. We have to create a dataframe in which we want to insert. We will be using iris dataset.  Reading a comma separated file as a dataframe in pandas.
import pandas as pd
df = pd.read_csv('iris.csv')
df.columns=['sepal_length', 'sepal_width', 'petal_length',
            'petal_width','variety']

print(df.head())
output
   sepal_length    sepal_width    petal_length   petal_width   variety
0   5.1             3.5            1.4            0.2           Setosa
1   4.9             3.0            1.4            0.2           Setosa
2   4.7             3.2            1.3            0.2           Setosa
3   4.6             3.1            1.5            0.2           Setosa

We have two approaches for adding a row to a dataframe i.e. using append() function and loc() function. If you want to know about loc() function follow here.

How to append row to a dataframe in python using append() function ?

For inserting a row  in dataframe using append() function. We have to create a dictionary with the key as column name of dataframe and values as we want  and pass it in append() function. For example, if we want to add a row in the above iris dataset.
# Add a new row to the DataFrame
new_row = {'sepal_length': 5.1, 'sepal_width': 3.5, 
         'petal_length': 1.4, 'petal_width': 0.2, 'variety': 'setosa'}
df = df.append(new_row, ignore_index=True)

# Display the updated DataFrame
print("\nUpdated DataFrame:")
print(df.tail())
Output
Updated DataFrame:
     sepal_length  sepal_width  petal_length  petal_width    variety
146           6.3          2.5           5.0          1.9  Virginica
147           6.5          3.0           5.2          2.0  Virginica
148           6.2          3.4           5.4          2.3  Virginica
149           5.9          3.0           5.1          1.8  Virginica
150           5.1          3.5           1.4          0.2     setosa

How to insert row to a dataframe in python using loc() function ?

There are 3 different manners to add a row in dataframe  using loc() method.

1. Using the loc indexer to add a row to a DataFrame.

Generally the loc() method is used to access a group of rows and columns by labels or a boolean array. We create a dictionary with key as column name and value as what we want. From that dictionary we create a pandas series object. If any column name is missing in the dictionary while creating a row, the row added would have None value for that column. 

Here loc function is used to specify a row with row label i.e. len(df). We create a pandas series object from the dictionary which is assigned to  ‘df.loc[len(df)]’ . We have printed last five rows to check if the new  row is added or not.

# New row data
row={'sepal_length': 5.5, 'sepal_width': 3.0, 'petal_length': 3.7,
    'petal_width': 0.9, 'variety': 'setosa'}
new_row = pd.Series(row)

# Add the new row using loc
df.loc[len(df)] = new_row
print(df.tail())

Output

     sepal_length  sepal_width  petal_length  petal_width    variety
147           6.5          3.0           5.2          2.0  Virginica
148           6.2          3.4           5.4          2.3  Virginica
149           5.9          3.0           5.1          1.8  Virginica
150           5.1          3.5           1.4          0.2     setosa
151           5.5          3.0           3.7          0.9     setosa

2. Using dictionary to add a row in a dataframe with loc() function.

We directly assign the dictionary having key as column and value what we want  to ‘df.loc[len(df)]’

# New row data
row={'sepal_length': 5.0, 'sepal_width': 3.0, 'petal_length': 3.0,
     'petal_width': 1.0, 'variety': 'setosa'}

# Add the new row using loc
df.loc[len(df)] = row
print(df.tail())

output

     sepal_length  sepal_width  petal_length  petal_width    variety
148           6.2          3.4           5.4          2.3  Virginica
149           5.9          3.0           5.1          1.8  Virginica
150           5.1          3.5           1.4          0.2     setosa
151           5.5          3.0           3.7          0.9     setosa
152           5.0          3.0           3.0          1.0     setosa

3. Using loc with a list to add a row.

Create a list with what value you want to add with respect to the sequence of columns in dataframe and assign it to  ‘df.loc[len(df)]’ and a new row will be added.
#New row data
row=[5.2, 3.2,  3.2, 1.2, 'setosa']

#Add the new row using loc
df.loc[len(df)] = row
print(df.tail())
output
     sepal_length  sepal_width  petal_length  petal_width    variety
149           5.9          3.0           5.1          1.8  Virginica
150           5.1          3.5           1.4          0.2     setosa
151           5.5          3.0           3.7          0.9     setosa
152           5.0          3.0           3.0          1.0     setosa
153           5.2          3.2           3.2          1.2     setosa

How to append a row to a empty dataframe in python?

Creating a blank dataframe with column name ‘A’,’B’ and ‘C’.
import pandas as pd
columns = ['A', 'B', 'C']  # Example column names
df = pd.DataFrame(columns=columns)
Now, adding a row using append function.
# New row to append
new_row = {'A': 1, 'B': 2, 'C': 3}  # Example values for the new row

# Append the new row to the empty DataFrame
df = df.append(new_row, ignore_index=True)
print(df)
output
   A  B  C
0  1  2  3

How to add rows to dataframe in loop?

To add multiple rows to a dataframe in a loop, we can iterate over our data and append each row to dataframe. I have create a list of dictionary. Where each dictionary stores information of each row which needs to be added to dataframe. We will use append function with for loop to achieve the required results.

import pandas as pd

# Create an empty DataFrame with column names
columns = ['A', 'B', 'C']  # Example column names
df = pd.DataFrame(columns=columns)

# Define your data to add (example list of dictionaries)
data_to_add = [
    {'A': 1, 'B': 2, 'C': 3},
    {'A': 4, 'B': 5, 'C': 6},
    {'A': 7, 'B': 8, 'C': 9}
]

# Iterate over the data and append each row to the DataFrame
for row_data in data_to_add:
    df = df.append(row_data, ignore_index=True)

# Display the updated DataFrame
print(df)

output

   A  B  C
0  1  2  3
1  4  5  6
2  7  8  9

How to add a blank row to a dataframe in python?

To insert a blank row, we will create a dictionary with keys as column name and value as None. The dictionary will be passed to append function which will add a blank row.
import pandas as pd

# Create an empty DataFrame with column names
columns = ['A', 'B', 'C']  # Example column names
df = pd.DataFrame(columns=columns)

# Create a dictionary representing a blank row
blank_row = {'A': None, 'B': None, 'C': None}
# Set values to None or any other desired blank value

# Append the blank row to the DataFrame
df = df.append(blank_row, ignore_index=True)
print(df)
Output
      A     B     C
0  None  None  None

Conclusion

We saw how to add a row to a dataframe in a easy manner. You can also learn how to use numpy where function with multiple conditions.

Leave a Comment

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