173

So I have initialized an empty pandas DataFrame and I would like to iteratively append lists (or Series) as rows in this DataFrame. What is the best way of doing this?

Wes Field
  • 3,291
  • 6
  • 23
  • 26

14 Answers14

184
df = pd.DataFrame(columns=list("ABC"))
df.loc[len(df)] = [1,2,3]
Ashot Matevosyan
  • 2,349
  • 1
  • 9
  • 11
179

Sometimes it's easier to do all the appending outside of pandas, then, just create the DataFrame in one shot.

>>> import pandas as pd
>>> simple_list=[['a','b']]
>>> simple_list.append(['e','f'])
>>> df=pd.DataFrame(simple_list,columns=['col1','col2'])
   col1 col2
0    a    b
1    e    f
Mike Chirico
  • 3,381
  • 1
  • 23
  • 20
71

Here's a simple and dumb solution:

>>> import pandas as pd
>>> df = pd.DataFrame()
>>> df = df.append({'foo':1, 'bar':2}, ignore_index=True)
Jaidev Deshpande
  • 3,016
  • 1
  • 16
  • 17
41

Could you do something like this?

>>> import pandas as pd
>>> df = pd.DataFrame(columns=['col1', 'col2'])
>>> df = df.append(pd.Series(['a', 'b'], index=['col1','col2']), ignore_index=True)
>>> df = df.append(pd.Series(['d', 'e'], index=['col1','col2']), ignore_index=True) 
>>> df
  col1 col2
0    a    b
1    d    e

Does anyone have a more elegant solution?

Alex Woolford
  • 4,433
  • 11
  • 47
  • 80
  • 1
    Here's a simpler and dumb solution: ``` import pandas as pd df = pd.DataFrame() df = df.append({'foo':1, 'bar':2}, ignore_index=True) # Note that this appending doesn't happen in place. ``` – Jaidev Deshpande Oct 11 '14 at 04:32
  • The `index` parameter should probably be named `columns`; `ignore_index` here ignores the index - the name of the Series. – flow2k Nov 30 '20 at 06:42
33

Following onto Mike Chirico's answer... if you want to append a list after the dataframe is already populated...

>>> list = [['f','g']]
>>> df = df.append(pd.DataFrame(list, columns=['col1','col2']),ignore_index=True)
>>> df
  col1 col2
0    a    b
1    d    e
2    f    g
Jay Marm
  • 556
  • 5
  • 12
21

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 with pandas.DataFrame.append().

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

Each of the above options should output something like:

>>> print (df)
   col1  col2
0     1     2
1     3     4
2     5     6

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

Fifi
  • 3,360
  • 2
  • 27
  • 53
8

Converting the list to a data frame within the append function works, also when applied in a loop

import pandas as pd
mylist = [1,2,3]
df = pd.DataFrame()
df = df.append(pd.DataFrame(data[mylist]))
janfelix
  • 99
  • 1
  • 1
  • 6
    What is `data[mylist]` ? Obviously, it throws the error `NameError: name 'data' is not defined`. Five votes show they know? – jaromrax Dec 21 '20 at 12:57
  • 2
    correct should be `df = df.append(pd.DataFrame(data=[mylist]))` . It is missing '=' after data. – Jakub Mar 18 '21 at 19:46
5

Here's a function that, given an already created dataframe, will append a list as a new row. This should probably have error catchers thrown in, but if you know exactly what you're adding then it shouldn't be an issue.

import pandas as pd
import numpy as np

def addRow(df,ls):
    """
    Given a dataframe and a list, append the list as a new row to the dataframe.

    :param df: <DataFrame> The original dataframe
    :param ls: <list> The new row to be added
    :return: <DataFrame> The dataframe with the newly appended row
    """

    numEl = len(ls)

    newRow = pd.DataFrame(np.array(ls).reshape(1,numEl), columns = list(df.columns))

    df = df.append(newRow, ignore_index=True)

    return df
jadki
  • 482
  • 1
  • 8
  • 15
4

If you want to add a Series and use the Series' index as columns of the DataFrame, you only need to append the Series between brackets:

In [1]: import pandas as pd

In [2]: df = pd.DataFrame()

In [3]: row=pd.Series([1,2,3],["A","B","C"])

In [4]: row
Out[4]: 
A    1
B    2
C    3
dtype: int64

In [5]: df.append([row],ignore_index=True)
Out[5]: 
   A  B  C
0  1  2  3

[1 rows x 3 columns]

Whitout the ignore_index=True you don't get proper index.

bmello
  • 1,864
  • 3
  • 18
  • 23
3

simply use loc:

>>> df
     A  B  C
one  1  2  3
>>> df.loc["two"] = [4,5,6]
>>> df
     A  B  C
one  1  2  3
two  4  5  6
Qinsi
  • 780
  • 9
  • 15
3

Consider an array A of N x 2 dimensions. To add one more row, use the following.

A.loc[A.shape[0]] = [3,4]
Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35
2

As mentioned here - https://kite.com/python/answers/how-to-append-a-list-as-a-row-to-a-pandas-dataframe-in-python, you'll need to first convert the list to a series then append the series to dataframe.

df = pd.DataFrame([[1, 2], [3, 4]], columns = ["a", "b"])
to_append = [5, 6]
a_series = pd.Series(to_append, index = df.columns)
df = df.append(a_series, ignore_index=True)
Abhishek Poojary
  • 749
  • 9
  • 10
0

The simplest way:

my_list = [1,2,3,4,5]
df['new_column'] = pd.Series(my_list).values

Edit:

Don't forget that the length of the new list should be the same of the corresponding Dataframe.

Minions
  • 5,104
  • 5
  • 50
  • 91
0

I will give an example with pd.concat as df.append is going to get deprecated,
Let's see how to append a list to an empty dataframe(just with column headers)

Create an empty dataframe onto which we will append the list

import pandas as pd  
columns=["Col1","Col2"]  
empty_df = pd.DataFrame(columns=columns)

empty_df

Append a list

list_to_append = [1,2]
dict_from_list= {k:v for k,v in zip(columns,list_to_append)}
df_for_list = pd.DataFrame(dict_from_list,index=[0])
concatenated_df = pd.concat([empty_df,df_for_list],ignore_index=True)

concatenated_df

newbie101
  • 65
  • 7