How to Append Row to pandas DataFrame

  • Post author:
  • Post category:Pandas / Python
  • Post last modified:January 18, 2022

You can append one row or multiple rows to an existing pandas DataFrame in several ways, one way would be creating a list or dict with the details and appending it to DataFrame.

You can append a row to DataFrame by using append(), pandas.concat(), and loc[], in this article I will explain how to append a python list, dict (dictionary) as a row to pandas DataFrame, which ideally inserts a new row(s) to the DataFrame with elements specified by a list and dict.

1. Quick Examples of Append Row to DataFrame

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


# Below are quick example

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

# Append 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 append 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)

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

# Append 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. Append Dict as Row to DataFrame

You can create a DataFrame and append 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 append dict as a row to DataFrame, not using this will get you an error.


# 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

3. Append List to DataFrame

If you have a list and want to append 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)

4. Append Row at the Specific Index Name

Note that in this example, first we are creating a new DataFrame/Series and append this to another DataFrame at the specified index.


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

# Append 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 Append

Use pd.concat([new_row,df.loc[:]]).reset_index(drop=True) to append the row to the first position of the DataFrame as Index starts from zero.


# Using pandas.concat() to append 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 Append Row

By using df.loc[Index_label]=new_row you can append a list as a row to the DataFrame at a specified index.


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

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

# Append 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 append a row to DataFrame using loc[], concat(), and append() methods. Using these you can append a row from list/dict at any position/index.

Happy Learning !!

You May Also Like

References

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

You are currently viewing How to Append Row to pandas DataFrame