There are several ways to append a list to a Pandas Dataframe in Python.
Option 1: append the list at the end of the dataframe with pandas.DataFrame.loc
:
df.loc[len(df)] = row
Option 2: convert the list to dataframe and concatenate it with pd.concat
to append a row:
row_df = pd.DataFrame([row], columns=df.columns)
df = pd.concat([df, row_df], ignore_index=True)
To append a list as a row to a Pandas DataFrame in Python, you can use the loc
method, the append
method (though deprecated in newer versions of Pandas), or convert the list to a DataFrame and concatenate it with the existing DataFrame. Below are the different methods:
loc
to Append a RowIf your DataFrame already exists and you want to append a row to it, you can use the loc
method.
import pandas as pd
# Create a DataFrame
df = pd.DataFrame(columns=["A", "B", "C"])
# List to append
row = [1, 2, 3]
# Append the list as a new row
df.loc[len(df)] = row
print(df)
pd.concat
to Append a RowYou can convert the list to a DataFrame and then concatenate it to the existing DataFrame.
import pandas as pd
# Create a DataFrame
df = pd.DataFrame(columns=["A", "B", "C"])
# List to append
row = [1, 2, 3]
# Convert list to DataFrame
row_df = pd.DataFrame([row], columns=df.columns)
# Append the new row
df = pd.concat([df, row_df], ignore_index=True)
print(df)
append
method (deprecated since version 1.4.0)The append
method was commonly used to add a row to a DataFrame, but it is deprecated since version 1.4.0.
import pandas as pd
# Create a DataFrame
df = pd.DataFrame(columns=["A", "B", "C"])
# List to append
row = [1, 2, 3]
# Append the list as a new row
df = df.append(pd.Series(row, index=df.columns), ignore_index=True)
print(df)
Let's consider the following dataframe and list:
import pandas as pd
# Dataframe
df = pd.DataFrame([[1, 2], [3, 4]], columns = ["col1", "col2"])
# List to append
list = [5, 6]
Using loc
:
>>> df.loc[len(df)] = list
>>> df
col1 col2
0 1 2
1 3 4
2 5 6
Using pd.concat
:
>>> row_df = pd.DataFrame([list], columns=df.columns)
>>> df = pd.concat([df, row_df], ignore_index=True)
>>> df
col1 col2
0 1 2
1 3 4
2 5 6
Using pd.append
:
>>> df = df.append(pd.Series(list, index=df.columns), ignore_index=True)
<stdin>:1: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
>>> df
col1 col2
0 1 2
1 3 4
2 5 6
loc
method: Suitable for directly adding rows based on the index.pd.concat
method: More flexible and suitable for larger DataFrames or when working with DataFrames.append
method: Deprecated, so prefer loc
or pd.concat
in newer code.The loc
and pd.concat
methods are the recommended ways to append rows to a DataFrame in Pandas 2.0 and later.
This chatbot (Alphonsio) provides automated responses generated by machine-learning algorithms and relies on the accuracy of the underlying language models. While this Chatbot is programmed to provide accurate and relevant information, its information may not always be exhaustive, accurate, up-to-date or tailored to individual circumstances. It is not a substitute for professional advice or consultation with qualified experts. This chatbots and its responses are intended for informational purposes only and should not be used for commercial or business purposes. The creators of this chatbot are not liable for any damages or losses incurred as a result of using the information provided. By using our website, you acknowledge and agree to these terms. The data you submit to this chatbot is used to improve our algorithms. Under no circumstances should you submit sensitive data such as, but not limited to, personal data or passwords. The data you submit could then be made public.