gpt4 book ai didi

python - 使用 pandas 选择数据的问题。国际劳工组织

转载 作者:行者123 更新时间:2023-11-30 22:07:07 24 4
gpt4 key购买 nike

这是示例数据框

ID,IS,Val1,Val2,Val3
1,100,11,9,1
2,101,3,15,16
3,99,10,18,3
1,97,29,25,26

我还使用 idxmin 来计算每行最小值,当我找到最小值时,我想检查与该列对应的最小值是否小于某个数字,如果是,那么我想包括其他值我想删除它。这就是我在 stack overflow 的帮助下所做的事情.

df1 = df.set_index('ID').iloc[:,1:].idxmin(axis=1).reset_index(name= 'New')

df2 = df1.loc[34 > df.iloc[:, 1:].min(1)]

我得到了这个结果

ID   New
1 Val3
2 Val1
3 Val3
1 Val2

当我使用此代码时,我也得到了相同的结果

df2 = df1.loc[34 > df.iloc[:, 3:].min(1)] # 在这段代码中,我从 Val2 开始我的专栏但它仍然给出相同的结果(包括 Val1)

ID   New
1 Val3
2 Val1
3 Val3
1 Val2

为什么即使我从第三列中进行选择,我也会得到相同的结果?这行代码到底在做什么? df1.loc[34 > df.iloc[:, 1:].min(1)]

最佳答案

您的 bool 条件对于每一行都返回全部 true,这就是为什么您有相同的结果

34 > df.iloc[:, 3:].min(1)
Out[202]:
0 True
1 True
2 True
3 True
dtype: bool
34 > df.iloc[:, 1:].min(1)
Out[203]:
0 True
1 True
2 True
3 True
dtype: bool

iloc 按位置对数据帧进行切片

df.iloc[:, 1:]
Out[204]:
IS Val1 Val2 Val3
0 100 11 9 1
1 101 3 15 16
2 99 10 18 3
3 97 29 25 26

关于python - 使用 pandas 选择数据的问题。国际劳工组织,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52522295/

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