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()]
这篇关于修改列表中的数据帧不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:修改列表中的数据帧不起作用
基础教程推荐
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
