26

I formulated this question about adding rows WITH index, but it is not yet clear to me how/why this happens when there are no indexes:

columnsList=['A','B','C','D']
df8=pd.DataFrame(columns=columnsList)
L=['value aa','value bb','value cc','value dd']
s = pd.Series(dict(zip(df8.columns, L)))
df8.append(s,ignore_index=True)
df8.append(s,ignore_index=True)

I EXPECT HERE A 2X4 DATAFRAME. nevertheless no values where added, nor an error occurred.

print(df8.shape)
#>>> (0,4)

Why is the series not being added, and why is not given any error?


If I try to add a row with LOC, an index is added,

df8.loc[df8.index.max() + 1, :] = [4, 5, 6,7]
print(df8)

result:

     A  B  C  D
NaN  4  5  6  7

I guess neither LOC, nor iLOC could be used to append rows without index name (i.e. Loc adds the index name NaN, and iLoc can not be used when the index number is higher than the rows of the database)

cs95
  • 379,657
  • 97
  • 704
  • 746
JFerro
  • 3,203
  • 7
  • 35
  • 88

2 Answers2

58

pandas >= 1.4

DataFrame.append has been deprecated, prefer pd.concat instead.

Note that in your example you're concatenating two series, so concatenate along axis=1 and transpose.

df = pd.concat([s, s], axis=1).T
df

          A         B         C         D
0  value aa  value bb  value cc  value dd
1  value aa  value bb  value cc  value dd

older versions

DataFrame.append is not an in-place operation. From the docs,

DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=None)

Append rows of other to the end of this frame, returning a new object. Columns not in this frame are added as new columns.

You need to assign the result back.

df = df8.append([s] * 2, ignore_index=True)
df
          A         B         C         D
0  value aa  value bb  value cc  value dd
1  value aa  value bb  value cc  value dd
cs95
  • 379,657
  • 97
  • 704
  • 746
  • 9
    "You need to assign the result back." was a life saver – Prashant Jul 01 '19 at 19:10
  • 1
    @SeanMcCarthy Where does it say that `df.append` works inplace? I have strong opinions on whether `inplace=True` is actually beneficial to the API or not. See [here](https://stackoverflow.com/a/60020384/4909087) – cs95 Dec 17 '20 at 18:17
  • 1
    @cs95 you're right. I was reading the example in the documentation wrong... my bad. In fact, it must be assigned to a variable, and cannot be done in-place. If it could be done in-place, there would likely be an argument of `inplace=True` as with other Pandas methods – Sean McCarthy Dec 17 '20 at 19:47
  • Note that it is now deprecated. See [doc](https://pandas.pydata.org/pandas-docs/version/1.5/reference/api/pandas.DataFrame.append.html?highlight=append#pandas.DataFrame.append) – Muhammad Yasirroni Apr 09 '23 at 07:17
  • @MuhammadYasirroni thank you, updated the answer to reflect this. – cs95 Apr 10 '23 at 14:14
13

The statement data.append(sub_data) does not work on its own.

But the statement data=data.append(sub_data) will work

Assigning it back solved the issue for me. Good tip not available elsewhere.

Tlaloc-ES
  • 4,825
  • 7
  • 38
  • 84
Rohit Dugal
  • 149
  • 1
  • 4