Appending to an empty DataFrame in Pandas?

Question:

Is it possible to append to an empty data frame that doesn’t contain any indices or columns?

I have tried to do this, but keep getting an empty dataframe at the end.

e.g.

import pandas as pd

df = pd.DataFrame()
data = ['some kind of data here' --> I have checked the type already, and it is a dataframe]
df.append(data)

The result looks like this:

Empty DataFrame
Columns: []
Index: []
Asked By: ericmjl

||

Answers:

This should work:

>>> df = pd.DataFrame()
>>> data = pd.DataFrame({"A": range(3)})
>>> df = df.append(data) 
>>> df

   A
0  0
1  1
2  2

Since the append doesn’t happen in-place, so you’ll have to store the output if you want it:

>>> df = pd.DataFrame()
>>> data = pd.DataFrame({"A": range(3)})
>>> df.append(data)  # without storing
>>> df
Empty DataFrame
Columns: []
Index: []
>>> df = df.append(data)
>>> df
   A
0  0
1  1
2  2
Answered By: DSM

And if you want to add a row, you can use a dictionary:

df = pd.DataFrame()
df = df.append({'name': 'Zed', 'age': 9, 'height': 2}, ignore_index=True)

which gives you:

   age  height name
0    9       2  Zed
Answered By: newmathwhodis

You can concat the data in this way:

InfoDF = pd.DataFrame()
tempDF = pd.DataFrame(rows,columns=['id','min_date'])

InfoDF = pd.concat([InfoDF,tempDF])
Answered By: Deepish

pandas.DataFrame.append Deprecated since version 1.4.0: Use concat() instead.

Therefore:

df = pd.DataFrame() # empty dataframe
df2 = pd..DataFrame(...) # some dataframe with data

df = pd.concat([df, df2])
Answered By: Wtower

The answers are very useful, but since pandas.DataFrame.append was deprecated (as already mentioned by various users), and the answers using pandas.concat are not "Runnable Code Snippets" I would like to add the following snippet:

import pandas as pd

df = pd.DataFrame(columns =['name','age'])
row_to_append = pd.DataFrame([{'name':"Alice", 'age':"25"},{'name':"Bob", 'age':"32"}])
df = pd.concat([df,row_to_append])

So df is now:

    name age
0  Alice  25
1    Bob  32
Answered By: Kloster Matias
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.