使附加到数据帧更有效的助手类
pandas-appender的Python项目详细描述
熊猫附属物
您是否曾想在Pandas数据帧中追加一堆行?结果证明 对于一个大的数据帧,这样做是非常低效的 多个数据帧和pd.concat公司而是他们。在
所以。。。助手函数?熊猫似乎没有。你自己滚? 好吧。这是助手函数。它可以追加大约100万英镑 每cpu每秒非常小的行数,并且有一个适度的额外内存 大约5兆字节的使用量,随着 追加行。在
安装
pip install pandas-appender
使用
from pandas_appender import DF_Appender
dfa = DF_appender(ignore_index=True) # note that ignore_index moves to the init
for i in range(1_000_000):
dfa = dfa.append({'i': i})
df = dfa.finalize()
类型提示和类别检测
使用较窄的类型和类别通常可以显著减小 数据帧。在pandas appender中有两种方法可以做到这一点。一是 附加到现有数据帧:
^{pr2}$第二个是传入一个dtypes=
参数:
dfa = DF_appender(ignore_index=True, dtypes=another_dataframe.dtypes)
pandas appender还提供了一种推断哪些列会更小的方法 如果它们是类别。此代码将分析现有的数据帧 附加到:
dfa = DF_appender(df, ignore_index=True, infer_categories=True)
或者它将分析第一段附加行:
dfa = DF_appender(ignore_index=True, infer_categories=True)
这些推断的类别将覆盖现有类型或dtypes=
参数。在
不兼容pandas.DataFrame.append()
在pandas.DataFame.append是幂等的,DF_Appender不是
- 熊猫:
df_new = df.append() # df is not changed
- 数据框附件:
dfa_new = dfa.append() # modifies dfa, and dfa_new == dfa
在pandas.DataFrame.append会提升类型,而DF_Appender是严格的
- Pandas:将
0.1
追加到一个整数列,该列将被提升为float - DF_Appender:当使用
dtypes=
或现有数据帧初始化时,附加0.1
到整数列导致{}被转换为整数,即 0
。在
- 项目
标签: