Pandas support several ways to append a list as a row to DataFrame, In this article, I will explain how to append a Python list as a row where it adds a new row to the DataFrame with elements specified by a list.
You can use the append()
method, loc[], and iloc[] properties to append/add a list of values as a row in Pandas, let’s see these with examples. Use loc vs iloc to understand the differences.
Key Points –
- Allows appending a list as a row at a specified index. Use
df.loc[len(df)] = list
to add the list at the end. - Enables setting values at specific integer-based index positions. Use
df.iloc[index] = list
to insert a row at a particular position. - The
append()
function can add a list as a row by converting it into a DataFrame or Series. - With
loc
oriloc
, you can place a new row at any desired position within the DataFrame. - When using
append()
, specifyignore_index=True
to reset the index after appending. - When using
append()
, convert the list to a DataFrame and set column names to ensure consistency.
Quick Examples of Pandas Append List to DataFrame
Below are some quick examples of appending a list as a row to DataFrame.
# Quick examples of pandas append list to DataFrame
# Example 1: New list to append row to DataFrame
list = ["Hyperion", 27000, "60days", 2000]
df.loc[len(df)] = list
# Example 2: Added at second position
df.iloc[1] = list
# Example 3: Using append()
list = ["Bigdata", 27000, "40days", 2800]
df2 = df.append(pd.DataFrame([list],
columns=["Courses","Fee","Duration","Discount"]),
ignore_index=True)
# Example 4: A series object with the same index as DataFrame
df2 = df.append(pd.Series(list, index = ["Courses","Fee","Duration","Discount"]),
ignore_index=True)
Let’s create a DataFrame with a few rows and columns then, execute some examples and validate the results. Our DataFrame contains column names Courses
, Fee
, Duration
and Discount
.
# Create DataFrame
import pandas as pd
technologies= {
'Courses':["Spark","PySpark","Hadoop","Python","Pandas"],
'Fee' :[22000,25000,23000,24000,26000],
'Duration':['30days','50days','35days', '40days','55days'],
'Discount':[1000,2300,1000,1200,2500]
}
df = pd.DataFrame(technologies)
print(" Create DataFrame:\n", df)
Yields below output.
Using loc[] to Append the New List to a DataFrame
By using df.loc[index]=list
you can append a list as a row to the DataFrame at a specified Index, In order to add at the end get the index of the last record using the len(df)
function. The below example adds the list ["Hyperion",27000,"60days",2000]
to the end of the Pandas DataFrame.
# New list to append Row to DataFrame
list = ["Hyperion", 27000, "60days", 2000]
df.loc[len(df)] = list
print("After appending a list as a row:\n", df)
Yields below output.
Use df.iloc[1]=list
to append the row to the second position of the DataFrame as the Index starts from zero.
# New list to append DataFrame
list = ["Oracle", 20000, "60days", 2000]
# using iloc[] method
df.iloc[1] = list
print("After appending a list as a row:\n", df)
Yields below output.
# Output:
# After appending a list as a row:
Courses Fee Duration Discount
0 Spark 22000 30days 1000
1 Oracle 20000 60days 2000
2 Hadoop 23000 35days 1000
3 Python 24000 40days 1200
4 Pandas 26000 55days 2500
Using DataFrame.append() to Add or Append a List as a Row
Alternatively, you can use DataFrame.append() function to add a row to the DataFrame. This by default adds a row at the end. You can Get Column Names as a List From Pandas DataFrame and use it in this example.
Related: You can also append two DataFrames using the append() function.
# New list for append into DataFrame
list = ["Bigdata", 27000, "40days", 2800]
# Using append to add the list to DataFrame
df2 = df.append(pd.DataFrame([list], columns=["Courses","Fee","Duration","Discount"]), ignore_index=True)
print("After appending a list as a row:\n", df2)
Yields below output.
# Output:
# After appending a list as a row:
Courses Fee Duration Discount
0 Spark 22000 30days 1000
1 PySpark 25000 50days 2300
2 Hadoop 23000 35days 1000
3 Python 24000 40days 1200
4 Pandas 26000 55days 2500
5 Bigdata 27000 40days 2800
Append Series as a Row to the DataFrame
Using append() you can also append series as a row to the DataFrame.
# New list for append into DataFrame
list = ["Bigdata", 27000, "40days", 2800]
# A series object with the same index as DataFrame
df2 = df.append(pd.Series(list, index = ["Courses","Fee","Duration","Discount"]), ignore_index=True)
print("After appending a list as a row:\n", df2)
Yields below output.
# Output:
# After appending a list as a row:
Courses Fee Duration Discount
0 Spark 22000 30days 1000
1 PySpark 25000 50days 2300
2 Hadoop 23000 35days 1000
3 Python 24000 40days 1200
4 Pandas 26000 55days 2500
5 Bigdata 27000 40days 2800
FAQ on Append a List as a Row to Pandas DataFrame
To append a list as a row to a Pandas DataFrame, you can use several methods, including loc
, append()
, or concat()
.
You can append multiple rows to a Pandas DataFrame from a list of lists. There are a few ways to do this, and the most efficient approach is to use pd.concat()
or create a new DataFrame from the list of lists and then concatenate it.
By default, Pandas automatically assigns the next available index. If you want to specify the index.
You can append a list as a row using the DataFrame.append()
method in Pandas, though this method is deprecated as of Pandas 1.4.0. It’s recommended to use pd.concat()
for appending rows in more recent versions of Pandas. However, if you are using a version of Pandas before 1.4.0, append()
will work fine.
You can append a list as a row to an empty Pandas DataFrame. Even when the DataFrame is empty, Pandas will automatically assign an index and the row will be added correctly.
Conclusion
In this article, you have learned how to append a list as a row to Pandas DataFrame using DataFrame.loc[]
, DataFrame.iloc[]
, and DataFrame.append()
methods. Using these you can append a row at any position/index.
Happy Learning !!
Related Articles
- Pandas join two DataFrames
- Pandas Merge Two DataFrames
- How to combine two DataFrames?
- Pandas Merge DataFrames on Index
- Append Pandas DataFrames Using for Loop
- How to Merge Series into Pandas DataFrame
- Pandas Merge DataFrames Explained Examples
- Pandas Append Rows & Columns to Empty DataFrame
.append for pd dfs is depreciated
.append for pd dfs is depreciated