Python 如何在pandas中获取满足一定条件的列索引?
我有以下资料:Python 如何在pandas中获取满足一定条件的列索引?,python,pandas,Python,Pandas,我有以下资料: x = pd.DataFrame({'a':[1,5,5], 'b':[7,0,7]}) 对于每一行,我想得到满足条件的第一列的索引,它的值大于某个值,比如大于 4. 在该示例中,答案是1(对应于第一行中值7的索引)和0(对应于第二行中值5的索引),以及1(对应于第三行中值5的索引)。 这意味着答案是[1,0,0] 我尝试了应用方法: def get_values_from_row(row, th=0.9): """Get a list of column names
x = pd.DataFrame({'a':[1,5,5], 'b':[7,0,7]})
对于每一行,我想得到满足条件的第一列的索引,它的值大于某个值,比如大于
4.
在该示例中,答案是1(对应于第一行中值7的索引)和0(对应于第二行中值5的索引),以及1(对应于第三行中值5的索引)。
这意味着答案是[1,0,0]
我尝试了应用方法:
def get_values_from_row(row, th=0.9):
"""Get a list of column names that meet some condition that their values are larger than a threshold.
Args:
row(pd.DataFrame): a row.
th(float): the threshold.
Returns:
string. contains the columns that it's value met the condition.
"""
return row[row > th].index.tolist()[0]
它可以工作,但我有一个大的数据集,而且速度很慢。
更好的选择是什么。我认为您需要:
IIUC输出不正确
[1,0,0]
?因为7
比4
高,这是正确的。我会更新。
print (x[x > 4])
a b
0 NaN 7.0
1 5.0 NaN
2 7.0 5.0
print (x[x > 4].apply(lambda x: x.index.get_loc(x.first_valid_index()), axis=1))
0 1
1 0
2 0
dtype: int64