If you have a list and want to append it to a pandas dataframe then this post is for you. In this tutorial, you will know the various ways to append a list to a pandas dataframe.
Method to append a list to a Pandas Dataframe
Let’s know all the methods.
Below is the sample list
sample_list= [40,"d",4]
Method 1: Using the loc indexer
The loc attribute in pandas allows you to access a selection of rows and columns in a DataFrame by their labels instead numerical index. You can use the loc accessor to go to the end of the dataframe or a particular location to append the list to the dataframe.
It also allows you to go to the position of the row using the index values.
If you want to append the new list to the end of the rows then use the len(df) to find the end of the index of the last row of the existing dataframe. After that, you can add your new list.
import pandas as pd
data= {"A":[10,20,30],"B":["a","b","c"],"C":[1,2,3]}
df = pd.DataFrame(data)
sample_list= [40,"d",4]
df.loc[len(df)] = sample_list
df
Output
A B C 0 10 a 1 1 20 b 2 2 30 c 3 3 40 d 4
Method 2: Using the append() method
You can also use the append() method. It joins the rows of two or more DataFrames. It combines the rows of one DataFrame with the bottom of another DataFrame and creates a new DataFrame with the union of the two. The original DataFrames stay the same and are not modified. The syntax for invoking this method is as follows
df1.append(df2)
Let’s append the the list to existing dataframe. Here you have to first convert the list to the dataframe and then append it to the existing dataframe.
Run the below lines of code to do so.
import pandas as pd
data= {"A":[10,20,30],"B":["a","b","c"],"C":[1,2,3]}
df1 = pd.DataFrame(data)
sample_list= [40,"d",4]
df2 = pd.DataFrame([sample_list], columns=df.columns)
df1 = df.append(df2,ignore_index=True)
df1
Output
A B C 0 10 a 1 1 20 b 2 2 30 c 3 3 40 d 4
Method 3: concat() method
The concat() method in pandas allows you to combine different DataFrames in either a row-wise or column-wise fashion. This function takes a list of DataFrames as an argument, as well as specifies the axis (either 0 for rows or 1 for columns) along which the DataFrames should be merged.
Use the below lines of code to append the list to the data frame.
import pandas as pd
data= {"A":[10,20,30],"B":["a","b","c"],"C":[1,2,3]}
df1 = pd.DataFrame(data)
sample_list= [40,"d",4]
df2 = pd.DataFrame([sample_list], columns=df.columns)
df1 = pd.concat([df1,df2],ignore_index=True)
df1
Output
A B C 0 10 a 1 1 20 b 2 2 30 c 3 3 40 d 4
Method 4: Using the pd.DataFrame.from_records method
You can also use the the pd.DataFrame.from_records() method to append the list to the DataFrame. It can be either a list of records or from a NumPy structured array. This method takes a list of records, with each record being represented as a dictionary, and transforms it into a DataFrame.
Then you can use the append() function ( method 1) to add list to the end.
Execute the below lines of code.
import pandas as pd
data= {"A":[10,20,30],"B":["a","b","c"],"C":[1,2,3]}
df1 = pd.DataFrame(data)
sample_list= [40,"d",4]
df2 = pd.DataFrame.from_records([sample_list], columns=df.columns)
df1 = df1.append(df2,ignore_index=True)
df1
Output
A B C 0 10 a 1 1 20 b 2 2 30 c 3 3 40 d 4
Conclusion
These are the four methods to append the list to the existing pandas dataframe. You can use the loc with the index to append the list at the desired location. Also, you can use the append() function of the pandas to append the list after the end row of the dataframe.
Leave a Reply