df.append()未追加到DataFrame [英] df.append() is not appending to the DataFrame

查看:848
本文介绍了df.append()未追加到DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制定了关于添加带有WITH索引的行的这个问题,但是我尚不清楚在没有索引的情况下,为什么/为什么发生这种情况:

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)

我希望这里有2X4数据帧. 但是,没有添加任何值,也没有发生错误.

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

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

为什么不添加系列,为什么没有出现任何错误?

如果我尝试使用LOC添加行,则会添加索引,

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)

结果:

     A  B  C  D
NaN  4  5  6  7

我想LOC和iLOC都不能用来添加没有索引名称的行(即Loc添加索引名称NaN,并且当索引号高于数据库行时不能使用iLoc)

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)

推荐答案

DataFrame.append 不是就地操作.在文档中,

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

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

将other的行追加到该帧的末尾,返回一个新对象. 不在此框架中的列将作为新列添加.

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.

df8 = df8.append([s] * 2, ignore_index=True)
df8
          A         B         C         D
0  value aa  value bb  value cc  value dd
1  value aa  value bb  value cc  value dd

这篇关于df.append()未追加到DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆