Pandas Add Row to DataFrame

There are multiple ways to add/insert a row to pandas DataFrame, in this article, I will explain how to add a row to DataFrame with several examples by using append(), pandas.concat(), and loc[]

The examples include how to insert/add a python list, dict (dictionary) as a row to pandas DataFrame, which ideally adds a new row to the DataFrame with elements specified by a list and dict.

1. Quick Examples of Add Row to 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

# add 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 pandas DataFrame from Dict 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. Add Dict as Row to DataFrame

You can create a DataFrame and add/insert a new row to this DataFrame from dict, first create a Python Dictionary and use append() function, this method is required to pass ignore_index=True in order to add dict as a row to DataFrame, not using this will get you an error. This 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 used ignore_index=True, it ignores the indexes on the DataFrame and set a new index.

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 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)

# Add row to the DataFrame using append()
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 concat() to Add a Row at Top of 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 of 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 loc[], concat(), and 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

You are currently viewing Pandas Add Row to DataFrame