285

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: []
Tommaso Di Noto
  • 928
  • 1
  • 11
  • 23
ericmjl
  • 12,073
  • 11
  • 48
  • 75
  • 1
    Answered a similar question here: http://stackoverflow.com/questions/13784192/creating-an-empty-pandas-dataframe-then-filling-it/41529411#41529411. 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 Feb 24 '17 at 05:31
  • **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 Aug 04 '19 at 13:46
  • 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](https://stackoverflow.com/questions/16641119/why-does-append-always-return-none-in-python) – smci Aug 04 '19 at 13:50

5 Answers5

506

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
cs95
  • 330,695
  • 80
  • 606
  • 657
DSM
  • 319,184
  • 61
  • 566
  • 472
  • 14
    Thank you! That worked! I didn't realize that I had to store the output... I probably should have read the documentation better, but I appreciate it, @DSM! – ericmjl May 16 '13 at 21:06
  • 16
    i always forget you need to assign it! – Andy B Aug 04 '14 at 19:07
  • 89
    actually that append doesn't happen in place is the most important info here ;) – refuzee Jun 30 '15 at 16:32
  • 10
    No clue why [Pandas](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.append.html) examples don't show that. Thanks for your help! – Drew Szurko Jul 15 '17 at 18:55
  • 5
    note that at least in june 2018 if you'd like the new rows to auto-index themselves, you should write df.append(data, ignore_index=True). Thanks for the great answer! – Adam B Jun 15 '18 at 19:04
  • To append in-place, check this [answer](https://stackoverflow.com/questions/19365513/how-to-add-an-extra-row-to-a-pandas-dataframe/19368360#19368360) – Anwarvic Jul 30 '18 at 13:33
  • thanks for mentioning, that update does not happen in-place. – D_K Jan 30 '19 at 08:54
  • Is there an easy way to know what operations are in place, and which aren't? In Python "everything is an object", so you'd think .append would be directly on the object and thus in place. – jparanich Oct 11 '19 at 05:25
  • You've saved me. Thank you – Jean Lima Oct 27 '20 at 19:51
  • Haha, the information about the inplace saved me – romanzdk Feb 12 '21 at 09:09
  • Had same issue. Forgot to store the data as the append did not happen in-place as mentioned. Answer still useful in 2022! – mapperx Jan 11 '22 at 07:06
  • `FutureWarning`: The `frame.append` method is deprecated and will be removed from pandas in a future version. Use `pandas.concat` instead. – Curious Watcher May 30 '22 at 10:31
125

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
dval
  • 2,999
  • 1
  • 22
  • 26
35

You can concat the data in this way:

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

InfoDF = pd.concat([InfoDF,tempDF])
Deepish
  • 651
  • 6
  • 10
3

I tried this way and it works

import pandas as pd

df = pd.DataFrame(columns =['columnA','columnB'])
data = {'columnA':'data', 'columnB':'data'}
df = df.append(data)
W Kenny
  • 1,389
  • 15
  • 25
2

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])
Wtower
  • 17,145
  • 11
  • 98
  • 72