Question #332   Submitted by Answiki on 12/31/2020 at 01:24:51 PM UTC

How to append a list as a row to a Pandas DataFrame in Python?

Answer   Submitted by Answiki on 12/31/2020 at 01:57:18 PM UTC

There are several ways to append a list to a Pandas Dataframe in Python. 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]


Option 1: append the list at the end of the dataframe with pandas.DataFrame.loc.

df.loc[len(df)] = list


Option 2: convert the list to dataframe and append with pandas.DataFrame.append().

df = df.append(pd.DataFrame([list], columns=df.columns), ignore_index=True)


Option 3: convert the list to series and append withpandas.DataFrame.append().

df = df.append(pd.Series(list, index = df.columns), ignore_index=True)


Each of the above options should create a dataframe similar to:

   col1  col2
0     1     2
1     3     4
2     5     6

7 events in history
Answer by Answiki on 12/31/2020 at 01:57:18 PM

There are several ways to append a list to a Pandas Dataframe in Python. 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]


Option 1: append the list at the end of the dataframe with pandas.DataFrame.loc.

df.loc[len(df)] = list


Option 2: convert the list to dataframe and append with pandas.DataFrame.append().

df = df.append(pd.DataFrame([list], columns=df.columns), ignore_index=True)


Option 3: convert the list to series and append withpandas.DataFrame.append().

df = df.append(pd.Series(list, index = df.columns), ignore_index=True)


Each of the above options should create a dataframe similar to:

   col1  col2
0     1     2
1     3     4
2     5     6

Question by Answiki 12/31/2020 at 01:45:28 PM
In Python, how to append a list as a row to a Pandas DataFrame ?
Answer by Answiki on 12/31/2020 at 01:43:02 PM

There are several ways to append a list to a Pandas Dataframe in Python. 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]


Option 1: append the list at the end of the dataframe with pandas.DataFrame.loc.

df.loc[len(df)] = list


Option 2: convert the list to dataframe and append with pandas.DataFrame.append().

df = df.append(pd.DataFrame([list], columns=df.columns), ignore_index=True)


Option 3: convert the list to series and append withpandas.DataFrame.append().

df = df.append(pd.Series(list, index = df.columns), ignore_index=True)


Each of the above options should create a dataframe similar to:

   col1  col2
0     1     2
1     3     4
2     5     6


Answer by Answiki on 12/31/2020 at 01:38:07 PM

There are several ways to append a list to a Pandas Dataframe in Python. 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]


Option 1: append the list at the end of the dataframe with pandas.DataFrame.loc.

df.loc[len(df)] = list


Option 2: convert the list to dataframe and append with pandas.DataFrame.append().

df = df.append(pd.DataFrame([list], columns=df.columns), ignore_index=True)


Option 3: convert the list to series and append withpandas.DataFrame.append().

df = df.append(pd.Series(list, index = df.columns), ignore_index=True)

Answer by Answiki on 12/31/2020 at 01:36:31 PM

There are several ways to append a list to a Pandas Dataframe in Python. 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]


Option 1: append the list at the end of the dataframe with pandas.DataFrame.loc.

df.loc[len(df)] = list


Option 2: convert the list to dataframe and append with pandas.DataFrame.append().

df = df.append(pd.DataFrame([list], columns=df.columns), ignore_index=True)


Option 3: convert the list to series and append withpandas.DataFrame.append()pandas.DataFrame.append().

df = df.append(pd.Series(list, index = df.columns), ignore_index=True)

Answer by Answiki on 12/31/2020 at 01:35:28 PM

There is several ways to append a list to a Pandas Dataframe in Python. 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]


Option 1: append the list at the end of the dataframe with pandas.DataFrame.loc.

df.loc[len(df)] = list


Option 2: convert the list to dataframe and append with pandas.DataFrame.append().

df = df.append(pd.DataFrame([list], columns=df.columns), ignore_index=True)


Option 3: convert the list to serie and append withpandas.DataFrame.append()pandas.DataFrame.append().

series = pd.Series(list, index = df.columns)
df = df.append(series, ignore_index=True)

Question by Answiki 12/31/2020 at 01:24:51 PM
How to append a list as a row to a Pandas DataFrame in Python?
# ID Query URL Count

Icons proudly provided by Friconix.