Pandas Insert or Add a Row to DataFrame Examples

You can use DataFrame.append(), pandas.concat(), DataFrame.loc[] methods to insert/add a row to pandas DataFrame. In this article, I will explain how to insert a python list, dict (dictionary) as a row, which ideally adds a new row to the DataFrame with elements specified by a list. There are several ways to do so, here I will cover using DataFrame.append(), DataFrame.loc[], pandas.concat() methods with examples.

1. Quick Examples of Insert Row to Pandas DataFrame

If you are in a hurry, below are some quick examples of how to insert/add a row to pandas DataFrame.


# Below are quick example

# New list to append Row to DataFrame
list_row = ["Hyperion", 27000, "60days", 2000]
df.loc[len(df)] = list_row

# Insert Dict to the dataframe using DataFrame.append()
new_row = {'Courses':'Hyperion', 'Fee':24000, 'Duration':'55days', 'Discount':1800}
df2 = df.append(new_row, ignore_index=True)

# Add new row to specifig index name
df2 = df.append(pd.DataFrame([new_row],index=['7'],columns=df.columns))

# Append row to the DataFrame
df2 = df.append(pd.Series(new_row, index=df.columns, name='7'))

# Using pandas.concat() to add a row
new_row = pd.DataFrame({'Courses':'Hyperion', 'Fee':24000, 'Duration':'55days', 'Discount':1800}, index=[0])
df2 = pd.concat([new_row,df.loc[:]]).reset_index(drop=True)

# Add specific row/index name using DataFrame.loc[]
df.loc['7', :] = ['Hive',25000,'45days',2000]

# Add row in DataFrame using DataFrame.loc[]
df.loc['7'] = ['Hive',25000,'45days',2000]

Let’s create a DataFrame with a few rows and columns and execute some examples to learn how to insert rows. Our DataFrame contains column names Courses, Fee, Duration, and Discount.


import pandas as pd
technologies = ({
    'Courses':["Spark","Hadoop","pandas","Java","Pyspark"],
    'Fee' :[20000,25000,30000,22000,26000],
    'Duration':['30days','40days','35days','60days','50days'],
    'Discount':[1000,2500,1500,1200,3000]
               })
df = pd.DataFrame(technologies)
print(df)

Yields below output.


   Courses    Fee Duration  Discount
0    Spark  20000   30days      1000
1   Hadoop  25000   40days      2500
2   pandas  30000   35days      1500
3     Java  22000   60days      1200
4  Pyspark  26000   50days      3000

2. Use DataFrame.append() to Insert Dict as Row to DataFrame

You can create a DataFrame and append a new row to this DataFrame. To do so, create a Python Dictionary and use append() function. While you are adding a Python Dictionary with append(), required to pass ignore_index=True, not using this will get you an error. append() method returns the new DataFrame with the newly added row.


# Insert row to the dataframe using DataFrame.append()
df = pd.DataFrame(technologies)
new_row = {'Courses':'Hyperion', 'Fee':24000, 'Duration':'55days', 'Discount':1800}
df2 = df.append(new_row, ignore_index=True)
print(df2)

Yields below output.


    Courses    Fee Duration  Discount
0     Spark  20000   30days      1000
1    Hadoop  25000   40days      2500
2    pandas  30000   35days      1500
3      Java  22000   60days      1200
4   Pyspark  26000   50days      3000
5  Hyperion  24000   55days      1800

Note that when you have a default number index, it automatically increments the index and adds the row at the end of the DataFrame.

3. Add or Insert List as Row to DataFrame

If you have a list and want to add/insert it to DataFrame use loc[]. For more similar examples, refer to how to append a list as a row to pandas DataFrame.


# New list to append Row to DataFrame
list = ["Hyperion", 27000, "60days", 2000]
df.loc[len(df)] = list
print(df)

Note that when you have a default number index, it automatically increments the index and adds the row at the end of the DataFrame.

4. Add New Row to Pandas DataFrame with Specific Index Name

Alternatively, you can also use DataFrame.append() function to add a new row to pandas DataFrame with a custom Index. Note that in this example, first we are creating a new DataFrame/Series and append this to another DataFrame.


# Add new row to specifig index name
df2 = df.append(pd.DataFrame([new_row],index=['7'],columns=df.columns))
print(df2)

# Append row to the DataFrame
df2 = df.append(pd.Series(new_row, index=df.columns, name='7'))
print(df2)

Yields below output for both examples.


    Courses    Fee Duration  Discount
0     Spark  20000   30days      1000
1    Hadoop  25000   40days      2500
2    pandas  30000   35days      1500
3      Java  22000   60days      1200
4   Pyspark  26000   50days      3000
7  Hyperion  24000   55days      1800

5. Use pandas.concat() to Add a Row at Top in pandas DataFrame

Use pd.concat([new_row,df.loc[:]]).reset_index(drop=True) to add the row to the first position of the DataFrame as Index starts from zero. reset_index() will reset the Index on the DataFrame to adjust the indexes on other rows.


# Using pandas.concat() to add a row
new_row = pd.DataFrame({'Courses':'Hyperion', 'Fee':24000, 'Duration':'55days', 'Discount':1800}, index=[0])
df2 = pd.concat([new_row,df.loc[:]]).reset_index(drop=True)
print (df2)

Yields below output.


    Courses    Fee Duration  Discount
0  Hyperion  24000   55days      1800
1     Spark  20000   30days      1000
2    Hadoop  25000   40days      2500
3    pandas  30000   35days      1500
4      Java  22000   60days      1200
5   Pyspark  26000   50days      3000

6. Use DataFrame.loc[] to Add Specific Row/Index Name

By using df.loc[Index_label]=new_row you can add a list as a row to the DataFrame at a specified index, In add at the end get the index of the last record using DataFrame.loc['Index5',:] function.


# Add specific row/index name using DataFrame.loc[]
df.loc['7', :] = ['Hive',25000,'45days',2000]
print(df)

# Add row in DataFrame using DataFrame.loc[]
df.loc['7'] = ['Hive',25000,'45days',2000]
print(df)

Yields below output.


         Courses      Fee Duration  Discount
0          Spark  20000.0   30days    1000.0
1         Hadoop  25000.0   40days    2500.0
2         pandas  30000.0   35days    1500.0
3           Java  22000.0   60days    1200.0
4        Pyspark  26000.0   50days    3000.0
7           Hive  25000.0   45days    2000.0

7. Complete Example For Add Row to DataFrame


import pandas as pd
technologies = ({
    'Courses':["Spark","Hadoop","pandas","Java","Pyspark"],
    'Fee' :[20000,25000,30000,22000,26000],
    'Duration':['30days','40days','35days','60days','50days'],
    'Discount':[1000,2500,1500,1200,3000]
               })
df = pd.DataFrame(technologies)
print(df)

# Insert row to the dataframe using DataFrame.append()
df = pd.DataFrame(technologies)
new_row = {'Courses':'Hyperion', 'Fee':24000, 'Duration':'55days', 'Discount':1800}
df2 = df.append(new_row, ignore_index=True)
print(df2)

# Add new row to specifig index name
new_row = {'Courses':'Oracle','Fee':25000,'Duration':'65days','Discount':2800}
df2 = df.append(pd.DataFrame([new_row],index=['Index'],columns=df.columns))
print(df2)

# Append row to the DataFrame
df2 = df.append(pd.Series(new_row, index=df.columns, name='Index'))
print(df2)

# Using pandas.concat() to add a row
new_row = pd.DataFrame({'Courses':'Hyperion', 'Fee':24000, 'Duration':'55days', 'Discount':1800}, index=[0])
df2 = pd.concat([new_row,df.loc[:]]).reset_index(drop=True)
print (df2)

# Add specific row/index name using DataFrame.loc[]
df.loc['Index5', :] = ['Hive',25000,'45days',2000]
print(df)

# Add row in DataFrame using DataFrame.loc[]
df.loc['Index5'] = ['Hive',25000,'45days',2000]
print(df)

Conclusion

In this article, you have learned how to insert/add a row to DataFrame using DataFrame.loc[], pandas.concat(), and DataFrame.append() methods. Using these you can add a row from list/dict at any position/index.

Happy Learning !!

You May Also Like

References

Leave a Reply

Pandas Insert or Add a Row to DataFrame Examples