检查与pd.idxmin()对应的值,并在某些条件下进行过滤 [英] Check values corresponding to the pd.idxmin() and filter with some conditions
问题描述
对不起,我找不到合适的标题。随时更改它。
Sorry, I couldnt find any good title for it. Feel free to change it.
这是我的示例数据框
ID,Val1,Val2,Val3
1,10,9,11
2,14,15,16
3,17,18,1
1,22,25,3
我想逐行检查除ID以外的每一列的值,以及是否有最小数量大于4,那么我想删除这些列。
在这种情况下,ID 1(top)和2的最小值大于4。因此,我想从数据帧中删除这两行。
I want to check row by row value of each columns except ID and if there is minimum number which is greater than 4, then I would like to drop those columns. Here in this case, ID 1(top) and 2 has the minimum number greater than 4. So I want to drop both of those rows from the dataframe.
我无法检查使用列名称,因为在我的实际数据框中,我将有100列列,甚至不知道它们的名称。
I cannot check using the column names because in my real dataframe, i would have 100s of columns and I dont even know their name.
要查找每行的最小值,我正在使用 idxmin
。
To find the minimum value per each row, I am using idxmin
.
df1 = df.set_index('ID').idxmin(axis=1).reset_index(name= 'New')
我有这个输出
ID New
1 Val2
2 Val1
3 Val3
1 Val3
我的问题是,有没有一种方法可以跟踪与这些新输出相对应的值,以便可以简单地应用(df1 ['新建]>一些条件)
并根据条件删除这些行。
My question is that, is there a way that I can track the value corresponding to these new output so that I can apply simply (df1['New] > some conditions)
and remove those rows based on the conditions.
例如,在这种情况下,最终输出为(删除那些大于4的最小行之后)
for example, in this case the final output would be (after dropping those rows minimum number greater than 4)
ID New
3 Val3
1 Val3
或者还有其他更简单的方法吗?
Or is there any other easier way ?
推荐答案
您可以对第一个数据帧进行 min
You can do min
with first dataframe
df1.loc[df.iloc[:,1:].min(1)<4]
Out[270]:
ID New
2 3 Val3
3 1 Val3
这篇关于检查与pd.idxmin()对应的值,并在某些条件下进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!