0

I am trying to append a new row to an empty dataset and i found the below code fine:

import panda as pd

df = pd.DataFrame(columns=['A'])
for i in range(5):
    df = df.append({'A': i}, ignore_index=True)

So, it gives me:

    A
0   0
1   1
2   2
3   3
4   4

But, when i try the below code, my dataset is still empty:

df = pd.DataFrame(columns=['A'])
df.append({'A': 2}, ignore_index=True)
df

Can someone explain me the solution to add only 1 row?

Jeff
  • 7,767
  • 28
  • 85
  • 138
  • Look at the first code and the second, and try to figure out the difference. – cs95 Jun 25 '19 at 01:29
  • 1
    [`DataFrame.append`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.append.html): "Append rows of other to the end of caller, **returning a new object.**", so it needs to be assigned back – ALollz Jun 25 '19 at 01:29
  • 1
    Assign it back ..:-) – BENY Jun 25 '19 at 01:29
  • @WeNYoBen I have been thinking about how I keep seeing append inside a loop. This question finally pushed me over the edge and made me write [this.](https://stackoverflow.com/questions/13784192/creating-an-empty-pandas-dataframe-then-filling-it/56746204#56746204) I hope I can use this answer to convince newbies not to keep doing this :) – cs95 Jun 25 '19 at 03:05

0 Answers0