maxframe.dataframe.Series.append#
- Series.append(other, ignore_index=False, verify_integrity=False, sort=False)#
将 other 的行追加到调用者的末尾,返回一个新对象。
other 中不在调用者中的列将作为新列添加。
- 参数:
- 返回:
一个新的 DataFrame,由调用者和 other 的行组成。
- 返回类型:
参见
concat用于连接 DataFrame 或 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