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?
14 Answers
df = pd.DataFrame(columns=list("ABC"))
df.loc[len(df)] = [1,2,3]

- 2,349
- 1
- 9
- 11
-
13
-
3
-
5
-
6This is generally ok but only works if you have a monotonically increasing index that started at 0. – dreab Jul 05 '19 at 09:26
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

- 3,381
- 1
- 23
- 20
Here's a simple and dumb solution:
>>> import pandas as pd
>>> df = pd.DataFrame()
>>> df = df.append({'foo':1, 'bar':2}, ignore_index=True)

- 3,016
- 1
- 16
- 17
-
41
-
Also note that pandas is going to remove df.append in near future. One should use pd.concat. I have written an answer with this suggestion – newbie101 Jun 16 '23 at 17:44
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?

- 4,433
- 11
- 47
- 80
-
1Here'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
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

- 556
- 5
- 12
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?

- 3,360
- 2
- 27
- 53
-
Why `df.loc` in the first option instead of `df.iloc`? – Danilo Matrangolo Marano May 21 '21 at 17:00
-
1@DaniloMatrangoloMarano check this question : https://stackoverflow.com/questions/31593201/how-are-iloc-and-loc-different – Fifi May 22 '21 at 14:41
-
2
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]))

- 99
- 1
- 1
-
6What 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
-
2correct should be `df = df.append(pd.DataFrame(data=[mylist]))` . It is missing '=' after data. – Jakub Mar 18 '21 at 19:46
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

- 482
- 1
- 8
- 15
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.

- 1,864
- 3
- 18
- 23
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

- 780
- 9
- 15
Consider an array A of N x 2 dimensions. To add one more row, use the following.
A.loc[A.shape[0]] = [3,4]

- 8,413
- 7
- 27
- 35

- 31
- 1
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)

- 749
- 9
- 10
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.

- 5,104
- 5
- 50
- 91
-
this works for a single instance of the list, but if you have multiple occurrences than my version works – Golden Lion Nov 14 '20 at 19:59
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)
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)

- 65
- 7