[英]Python dataframe get the row and column of one value
b1 b2
0 0 1
1 2 3
2 4 5
For example, we have a dataframe a in python, is that possible to get value (b1,2) when you input 4, and get value (b2,2) when input value 5. 例如,我们在python中有一个数据框a,当您输入4时可以获取值(b1,2),而当输入值5时可以获取值(b2,2)。
Also there's duplicate value in the dataframe, i want to get all their location. 在数据框中也有重复的值,我想获取它们的所有位置。
Option 1 选项1
one-line 一条线
Not recommended! 不建议!
a.unstack().eq(4).compress(lambda x: x).index.to_series().values[0]
('b1', 2)
Option 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)
Option 3 选项3
s = a.unstack()
pd.Series(s.index.values, s.values).loc[4]
('b1', 2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.