gpt4 book ai didi

python - 如何根据列中唯一值的数量从字典中删除/删除数据帧?

转载 作者:太空宇宙 更新时间:2023-11-04 11:12:27 24 4
gpt4 key购买 nike

我有一个包含 12 个 dfs 的字典,名为 f,每个 df 都有相同的列:BacksGas_Flow_sccm、ContextID、StepID, Time_Elapsed、iso_forest、警报

我想做的是计算 BacksGas_Flow_sccm 列中唯一值的数量,如果数字 id 唯一值小于 10,我想删除 df 来自字典。

我试过这样做:

v = list(f.values())

for i in range (0, len(v)):
if (v[i]['BacksGas_Flow_sccm'].nunique()) < 10:
del (v[i])

但我收到以下错误:

Traceback (most recent call last):

File "<ipython-input-8-580f2e039fa5>", line 2, in <module>
if (v[i]['BacksGas_Flow_sccm'].nunique()) < 10:

IndexError: list index out of range

关于如何执行此操作有什么建议吗?

附言:

所有dfsBacksGas_Flow_sccm中唯一值的数量

df    unique values
1 2
2 5
3 373
4 2
5 4
6 3
7 4
8 560
9 141
10 4
11 5
12 4

我想删除字典中除第 3、8 和 9 之外的所有 dfs

我知道分享dfs的截图不是一件好事,但是由于有多个dfs,所以很难发布一个示例df 这里。但这是我的 dfs 字典的样子

enter image description here

最佳答案

Celius Stingher 关于错误原因的说法是正确的

I think that after deleting an item in the loop, the index resets and that's why you might be getting the error [....]

这应该有效。

dictNew = dict()
for key,value in f.items():
if (value['BacksGas_Flow_sccm'].nunique()>=10):
dictNew[key] = value
f = dictNew

关于python - 如何根据列中唯一值的数量从字典中删除/删除数据帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57876318/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com