Modifying dataFrames inside a list is not working(修改列表中的数据帧不起作用)
问题描述
我有两个 DataFrames,我想执行相同的清理操作列表.我意识到我可以合并为一个,并且一次通过所有内容,但我仍然很好奇为什么这种方法不起作用
I have two DataFrames and I want to perform the same list of cleaning ops.
I realized I can merge into one, and to everything in one pass, but I am still curios why this method is not working
test_1 = pd.DataFrame({
"A": [1, 8, 5, 6, 0],
"B": [15, 49, 34, 44, 63]
})
test_2 = pd.DataFrame({
"A": [np.nan, 3, 6, 4, 9, 0],
"B": [-100, 100, 200, 300, 400, 500]
})
假设我只想获取没有 NaNs 的原始数据:我试过了
Let's assume I want to only take the raws without NaNs: I tried
for df in [test_1, test_2]:
df = df[pd.notnull(df["A"])]
但 test_2 保持不变.另一方面,如果我这样做:
but test_2 is left untouched. On the other hand if I do:
test_2 = test_2[pd.notnull(test_2["A"])]
现在我的第一个 raw 走了.
Now I the first raw went away.
推荐答案
所有这些切片/索引操作都会创建原始数据帧的视图/副本,然后您 重新分配 df到这些视图/副本,这意味着原件根本没有被触及.
All these slicing/indexing operations create views/copies of the original dataframe and you then reassign df to these views/copies, meaning the originals are not touched at all.
选项 1dropna(...inplace=True)
尝试就地 dropna 调用,这应该就地修改原始对象
Option 1
dropna(...inplace=True)
Try an in-place dropna call, this should modify the original object in-place
df_list = [test_1, test_2]
for df in df_list:
df.dropna(subset=['A'], inplace=True)
请注意,这是其中一次我会推荐就地修改,特别是因为这个用例.
Note, this is one of the few times that I will ever recommend an in-place modification, because of this use case in particular.
选项 2enumerate 重新赋值
或者,您可以重新分配到列表 -
Option 2
enumerate with reassignment
Alternatively, you may re-assign to the list -
for i, df in enumerate(df_list):
df_list[i] = df.dropna(subset=['A']) # df_list[i] = df[df.A.notnull()]
这篇关于修改列表中的数据帧不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:修改列表中的数据帧不起作用
基础教程推荐
- 求两个直方图的卷积 2022-01-01
- 包装空间模型 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
