In this post, we are going to understand how to Append List as a row in Pandas Dataframe with example by using different dataframe method that includes append(),iloc[],loc[], and concat().To run all the program in this post make sure pandas is installed on the system or else can be installed using “pip install pandas”
1. Append list as a row in Pandas Dataframe
The Pandas Append() method append rows of other dataframe at the end of the given dataframe. It does not change the original dataframe instead returns a new object. In this example, new rows are initialized as a list, and set ignore_index=True, setting ignore_index=False, will raise an error.” TypeError(“Can only append a dict if ignore_index=True”) TypeError: Can only append a dict if ignore_index=True.Let us understand with the below example
import numpy as np
import pandas as pd
dfobj = pd.DataFrame([['Dev','comp',100],['Max','Phy',100],['jack','Math',100]],columns=['Name','Subj','Marks'])
lst_appnd = [['Musk','Math',100]]
dfobj = dfobj.append(pd.DataFrame(lst_appnd,
columns=[ 'Name', 'Subj', 'Marks']),
ignore_index = True)
print(dfobj)
Output
Name Subj Marks
0 Dev comp 100
1 Max Phy 100
2 jack Math 100
3 Musk Math 100
2. iloc[] to Append List as a row in Pandas Dataframe
We can use the Dataframe iloc[] attribute to add a row at a specific index position in the dataframe.In this example, we are inserting a row at index position 0. We can insert rows at the top or bottom or any index of the dataframe by simply using the index.
Python Program to insert list as specific index
import pandas as pd
df_studs = pd.DataFrame(columns=['Name','Subj','Marks'],index=['Row_1', 'Row_2', 'Row_3'])
df_studs.iloc[0] = ['jack', 'Math', 100]
print(df_studs)
Output
Name Subj Marks
Row_1 jack Math 100
Row_2 NaN NaN NaN
Row_3 NaN NaN NaN
3. loc() to append list as row at specific Index
The loc() attribute of pandas dataframe is used to access a group of rows and columns by labels or boolean array, We can use DataFrame.loc() to access particular cell by row index and column label.
- We can get the numbers of rows by using the len(dataframe. index) method to find out the index at which we have to insert a new row.
- In this example, we have created an empty data frame(df_studs). To get the length we are using the len() method to find the index on which we have to insert rows.
import pandas as pd
dfobj = pd.DataFrame([['Dev','comp',100],['Max','Phy',100],['jack','Math',100]],columns=['Name','Subj','Marks'])
dfobj.loc[len(dfobj.index)] = ['Musk', 'Math', 93]
print(f'\n{dfobj }')
Output
Name Subj Marks
0 Dev comp 100
1 Max Phy 100
2 jack Math 100
3 Musk Math 93
4. concat() to append list as row to dataframe
The pandas concat() method “Concatenate pandas objects along a particular axis with optional set logic along the other axes.
- In this example, we have already a dataframe dfobj that contain some data.
- We are appending the list to the existing dataframe by converting both lists to dataframe.
- Finally using the pd. concate() method to concat existing dataframe to existing dataframe
dfobj = pd.DataFrame([['Dev','comp',100],['Max','Phy',100],['jack','Math',100]],columns=['Name','Subj','Marks'])
list1 = pd.DataFrame(data=np.array([['Eon','Math',100]]), columns=['Name','Subj','Marks'])
list2 = pd.DataFrame(data=np.array([['Buse','Phy',100]]), columns=['Name','Subj','Marks'])
dfobj = pd.concat([dfobj,list1,list1], ignore_index=True)
print(dfobj)
Output
Name Subj Marks
0 Dev comp 100
1 Max Phy 100
2 jack Math 100
3 Eon Math 100
4 Eon Math 100
Summary
In this post we have different ways of how to Append List as a row in Pandas Dataframe by using different pandas dataframe method that includes append(),iloc[],loc[], and concat() with example.