Pandas 通过索引选择器从 DataFrame 中查找值
假设我们有一个带有任意但长列数的索引数据框:
from numpy.random import randint
import pandas as pd
df = pd.DataFrame(randint(0,100,size=(10, 4)), columns=list('ABCD'))
print(df)
> A B C D
> 0 78 1 97 98
> 1 93 58 46 45
> 2 50 1 77 27
> 3 63 87 66 21
> 4 26 1 10 46
> 5 26 60 71 79
> 6 74 4 62 98
> 7 93 22 23 89
> 8 30 31 14 46
> 9 51 4 90 22
并且有一个选择器,其中包含每列所需的索引,例如:
selector = pd.DataFrame({ "other_index": randint(len(df.index),size=len(df.columns))},
index=df.columns)
print(selector)
> other_index
> A 9
> B 0
> C 3
> D 4
现在我想得到
selected = [df[c].loc[selector.loc[c][0]] for c in df.columns]
print(selected)
> [51, 1, 66, 46]
我很确定那里是 pandas 中实现此目的的更有效方法,但我找不到。
Suppose we have an indexed Dataframe with arbitrary but long number of columns:
from numpy.random import randint
import pandas as pd
df = pd.DataFrame(randint(0,100,size=(10, 4)), columns=list('ABCD'))
print(df)
> A B C D
> 0 78 1 97 98
> 1 93 58 46 45
> 2 50 1 77 27
> 3 63 87 66 21
> 4 26 1 10 46
> 5 26 60 71 79
> 6 74 4 62 98
> 7 93 22 23 89
> 8 30 31 14 46
> 9 51 4 90 22
And have a selector
, which contains which index need for each columns, like:
selector = pd.DataFrame({ "other_index": randint(len(df.index),size=len(df.columns))},
index=df.columns)
print(selector)
> other_index
> A 9
> B 0
> C 3
> D 4
Now I would like to get the
selected = [df[c].loc[selector.loc[c][0]] for c in df.columns]
print(selected)
> [51, 1, 66, 46]
I'm pretty sure there is a more efficient way in pandas to achieve this, but I can't find.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会在 df.lookup 将来被弃用之前使用它。 :)
I would use df.lookup before it got deprecated in the future. :)
IIUC,你可以
stack
和切片:输出:
[51, 31, 46, 46]
IIUC, you could
stack
and slice:output:
[51, 31, 46, 46]