【发布时间】:2022-02-16 04:01:17
【问题描述】:
我已经定义了一个空数据框
df = pd.DataFrame(columns=['Name', 'Weight', 'Sample'])
并希望在 for 循环中添加行,如下所示:
for key in my_dict:
...
row = {'Name':key, 'Weight':wg, 'Sample':sm}
df = pd.concat(row, axis=1, ignore_index=True)
但我得到了这个错误
cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid
如果我使用df = df.append(row, ignore_index=True),它可以工作,但似乎append 已被弃用。所以,我想使用concat()。我该如何解决?
【问题讨论】:
-
如果你收集一个列表中的所有字典并连接一次,效率会更高。