python - 将新行分配给大熊猫 DataFrame 导致 OOM
问题描述
我有一个df
像这样的DataFrame:
0 1 2 3 4 5 ... 1154161
1 a b c d e f ... A
2 g h i j k l ... B
3 m n o p q r ... C
...
86405 Q V W X Y Z ... ZY
这是一个86405 rows × 1154161 columns
数据框。请注意,索引从 开始1
。我正在尝试分配一行index=0
:
df.loc[0] = 0
但我遇到了错误:
MemoryError: Unable to allocate 372. GiB for an array with shape (99725281205,) and data type float32
我希望它看起来像:
0 1 2 3 4 5 ... 1154161
0 0 0 0 0 0 0 ... 0 <--- add this row
1 a b c d e f ... A
2 g h i j k l ... B
3 m n o p q r ... C
...
86405 Q V W X Y Z ... ZY
是否有另一种分配方式而不会耗尽内存?也许是大块的(最好不是)?
编辑:根据@hpaulj 请求添加 DataFrame 信息。
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1154161 entries, 0 to 1154160
Columns: 86405 entries, 1 to 86405
dtypes: float32(86405)
memory usage: 371.5 GB
EDIT2:请注意,示例 DataFrame 中的字母实际上是数字(float32)
解决方案
1.https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#setting-with-enlargement
df.loc[len(df)] = 0
print (df)
2.https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.append.html
df = df.append(pd.Series(0, index=df.columns), ignore_index=True)
资料来源: 使用熊猫在数据框中添加一个空行
推荐阅读
- django - 如何在 django 中实现这个自定义模型管理器?
- botframework - 如何使用自适应卡对消息扩展中的下拉选择执行操作
- vb.net - 有没有办法在排序项目后保持列表框的索引顺序?
- php - 无法在 laravel 8 上执行自动完成 - 未找到基表或视图:1146 表“workshop.nama_suppliers”不存在
- html - Bootstrap 仅适用于我网站的主页,但不适用于其他 html 页面。为什么?
- latex - 在 LaTeX 中添加参考和附录部分时,编译它会将附录中的一些(但不是全部)项目放在参考之前
- oracle - 用于插入数据的 Oracle Apex while 循环
- r - 如何用 R 绘制一个简单的矩形
- python - 将pdf转换为base64并附加到json数组中
- firefox-addon-webextensions - 匹配 urlpatterns 和属性的 webextensions