我有两个数据框说df and thresh_df
。的形状df
的是说1000*200
和thresh_df
是1*200
。
我需要分别将thresh_df
行与df
元素明智的每一行进行比较,并且必须获取其值小于的对应列号thresh_df
。
我尝试了以下
compared_df = df.apply(lambda x : np.where(x < thresh_df.values))
但是我得到一个空的数据框!如果问题不清楚,需要任何解释,请在评论中让我知道。
我认为apply并不是必须的,只需Series
选择第一行来比较转换为DataFrame的一行:
df = pd.DataFrame({
'B':[4,5,4,5,5,4],
'C':[7,8,9,4,2,3],
'D':[1,3,5,7,1,0],
'E':[5,3,6,9,2,4],
})
thresh_df = pd.DataFrame({
'B':[4],
'C':[7],
'D':[4],
'E':[5],
})
compared_df = df < thresh_df.iloc[0]
print (compared_df)
B C D E
0 False False True False
1 False False True True
2 False False False False
3 False True False False
4 False True True True
5 False True True True
然后使用每行DataFrame.any
至少一个True
过滤器并过滤索引值:
idx = df.index[compared_df.any(axis=1)]
print (idx)
Int64Index([0, 1, 3, 4, 5], dtype='int64')
详细说明:
print (compared_df.any(axis=1))
0 True
1 True
2 False
3 True
4 True
5 True
dtype: bool
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句