[英]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.