[英]Python dataframe get the row and column of one value
b1 b2
0 0 1
1 2 3
2 4 5
例如,我们在python中有一个数据框a,当您输入4时可以获取值(b1,2),而当输入值5时可以获取值(b2,2)。
在数据框中也有重复的值,我想获取它们的所有位置。
选项1
一条线
不建议!
a.unstack().eq(4).compress(lambda x: x).index.to_series().values[0]
('b1', 2)
选项2
np.where
i, j = np.where(a == 4)
# or faster with
# i, j = np.where(a.values == 4)
(a.columns[j[0]], a.index[i[0]])
('b1', 2)
选项3
s = a.unstack()
pd.Series(s.index.values, s.values).loc[4]
('b1', 2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.