To handle tabular data in python, we normally use dataframes. In this article, we will discuss how we can append a new row in a dataframe.
Append a New Row in a Dataframe Using the loc[] Attribute
If we have a row given in the form of a list, we can use the loc[] property defined in the pandas module to add the row to the dataframe. The loc property is used to get the row at a specific position in a dataframe. When invoked on a dataframe, it takes the row number as the input inside the square brackets and returns a slice of the dataframe.
To append the list as a new row into the dataframe, we will assign the list to the slice at the last row of the dataframe.
To get the position of the last row in the dataframe, we can find the length of the dataframe using the len() function. After getting the length of the dataframe, we can add the list as a new row to the dataframe as shown below.
import pandas as pd
df = pd.read_csv('Demo.csv')
print("The dataframe before the append operation:")
print(df)
values = [10, "Sam", "Typescript"]
length = len(df)
df.loc[length] = values
print("The dataframe after the append operation:")
print(df)
Output:
The dataframe before the append operation:
Roll Name Language
0 1 Aditya Python
1 2 Sam Java
2 3 Chris C++
The dataframe after the append operation:
Roll Name Language
0 1 Aditya Python
1 2 Sam Java
2 3 Chris C++
3 10 Sam Typescript
In this approach, we have appended a list as a new row to the data frame. Now, let us discuss an approach to append a python dictionary to the dataframe.
Append a New Row in a Dataframe Using the append() Method
If we are given a dictionary in which the keys of the dictionary consist of the column names of the dataframe, we can add the dictionary as a row into the dataframe using the append()
method. The append() method, when invoked on a dataframe, takes a python dictionary as its input argument and appends the values of the dictionary to the dataframe in the last row. Also, we need to assign the value True to the ignore_index parameter of the dataframe. After execution, the append() method returns the updated dataframe. You can observe this in the following example.
import pandas as pd
df = pd.read_csv('Demo.csv')
print("The dataframe before the append operation:")
print(df)
valueDict = {'Roll': 15, 'Name': "Wilson", 'Language': "Golang"}
length = len(df)
df = df.append(valueDict, ignore_index=True)
print("The dataframe after the append operation:")
print(df)
Output:
The dataframe before the append operation:
Roll Name Language
0 1 Aditya Python
1 2 Sam Java
2 3 Chris C++
The dataframe after the append operation:
Roll Name Language
0 1 Aditya Python
1 2 Sam Java
2 3 Chris C++
3 15 Wilson Golang
Conclusion
In this article, we have discussed two ways to append a new row in a dataframe in python. To learn more about python programming, you can read this article on list comprehension in python. You might also like this article on dictionary comprehension in python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.