这里采用append()函数。
添加一行
>>> res = pd.DataFrame(columns=('lib', 'qty1', 'qty2'))
>>> res = res.append([{'qty1':10.0}], ignore_index=True)
>>> print(res.head())
lib qty1 qty2
0 NaN 10.0 NaN
合并两个DataFrame
这里需要注意的是ignore_index参数。
ignore_index = False
>>> df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))
A B
0 1 2
1 3 4
>>> df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'))
>>> df.append(df2)
A B
0 1 2
1 3 4
0 5 6
1 7 8
ignore_index = True
>>> df.append(df2, ignore_index=True)
A B
0 1 2
1 3 4
2 5 6
3 7 8
使用Pandas append()函数
本文介绍了如何在Python中使用Pandas库的append()函数来添加数据行或合并DataFrame。详细解释了ignore_index参数的作用,当设置为True时,会重新生成索引,避免重复。
4689

被折叠的 条评论
为什么被折叠?



