maxframe.dataframe.DataFrame.append#

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

other 的行追加到调用者的末尾,返回一个新对象。

other 中不在调用者中的列将作为新列添加。

参数:
  • other (DataFrame or Series/dict-like object, or list of these) -- 要追加的数据。

  • ignore_index (bool, default False) -- 如果为 True,则结果轴将标记为 0, 1, …, n - 1。

  • verify_integrity (bool, default False) -- 如果为 True,在创建具有重复值的索引时将引发 ValueError。

  • sort (bool, default False) -- 如果 selfother 的列未对齐,则对列进行排序。

返回:

一个由调用者和 other 的行组成的新 DataFrame。

返回类型:

DataFrame

参见

concat

用于连接 DataFrame 或 Series 对象的通用函数。

备注

如果传递了 dict/series 列表且键都包含在 DataFrame 的索引中,则结果 DataFrame 中的列顺序将保持不变。

迭代地向 DataFrame 追加行可能比一次性连接更消耗计算资源。更好的解决方案是将这些行追加到列表中,然后一次性将列表与原始 DataFrame 连接。

示例

>>> import maxframe.dataframe as md
>>> df = md.DataFrame([[1, 2], [3, 4]], columns=list('AB'), index=['x', 'y'])
>>> df.execute()
   A  B
x  1  2
y  3  4
>>> df2 = md.DataFrame([[5, 6], [7, 8]], columns=list('AB'), index=['x', 'y'])
>>> df.append(df2).execute()
   A  B
x  1  2
y  3  4
x  5  6
y  7  8

ignore_index 设置为 True 时:

>>> df.append(df2, ignore_index=True).execute()
   A  B
0  1  2
1  3  4
2  5  6
3  7  8

以下内容虽然不推荐作为生成 DataFrame 的方法,但展示了从多个数据源生成 DataFrame 的两种方式。

效率较低的方式:

>>> df = md.DataFrame(columns=['A'])
>>> for i in range(5):
...     df = df.append({'A': i}, ignore_index=True)
>>> df.execute()
   A
0  0
1  1
2  2
3  3
4  4

更高效的方式:

>>> md.concat([md.DataFrame([i], columns=['A']) for i in range(5)],
...           ignore_index=True).execute()
   A
0  0
1  1
2  2
3  3
4  4