319

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
  • 1,208
  • 1
  • 13
  • 24
ericmjl
  • 13,541
  • 12
  • 51
  • 80
  • 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
  • "Why am I getting "AttributeError: 'DataFrame' object has no attribute 'append'?" From pandas 2.0, `append` was silently removed from the API to discourage people from iteratively growing DataFrames inside a loop :D `append` inside a loop is quadratic memory usage, so the suggested approach is to accumulate individual rows or DataFrames inside a python list and then convert it into one big df at the end. [More information in my answer](https://stackoverflow.com/a/76020749/4909087) – cs95 Apr 19 '23 at 08:18

6 Answers6

536

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
Alex Punnen
  • 5,287
  • 3
  • 59
  • 71
DSM
  • 342,061
  • 65
  • 592
  • 494
  • 6
    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
  • 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
  • 1
    `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
135

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
newmathwhodis
  • 3,209
  • 2
  • 24
  • 26
46

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
  • 746
  • 7
  • 10
15

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
Kloster Matias
  • 423
  • 4
  • 9
5

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
  • 18,848
  • 11
  • 103
  • 80
1

Why am I getting "AttributeError: 'DataFrame' object has no attribute 'append'?

Starting from pandas >= 2.0, append has been removed, use pd.concat instead.

The idiomatic way to append DataFrames (it to collect all your smaller DataFrames into a list, and then make one single call to pd.concat.

df_list = []
for df in some_function_that_yields_df():
    df_list.append(df)

big_df = pd.concat(df_list)

The rationale for its removal was to discourage iteratively growing DataFrames in a loop (which is what people typically use append for). This is because append makes a new copy at each stage, resulting in quadratic complexity in memory. See Creating an empty Pandas DataFrame, and then filling it (in particular, this answer) where answers discourage iteratively growing DataFrames.

More info:

cs95
  • 379,657
  • 97
  • 704
  • 746