258
votes

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: []
3
Answered a similar question here: stackoverflow.com/questions/13784192/…. basically something like this newDF = pd.DataFrame() #creates a new dataframe that's empty newDF = newDF.append(oldDF, ignore_index = True) # ignoring index is optional - geekidharsh
Append what? A single value? a Python list? a pandas Series? Another Dataframe? Your example trailing comment suggests you mean another dataframe - so give a dataframe in your example code, already :) - smci
And when you say "The result looks like this", I hope you're not trying to directly do print(df.append(data)), because append() always returns None in Python - smci

3 Answers

465
votes

That should work:

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

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

>>> df
Empty DataFrame
Columns: []
Index: []
>>> df = df.append(data)
>>> df
   A
0  0
1  1
2  2
113
votes

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
26
votes

You can concat the data in this way:

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

InfoDF = pd.concat([InfoDF,tempDF])