我有以下 df:
country sport score
0 ita swim 15
1 fr run 25
2 ger golf 37
3 ita run 17
4 fr golf 58
5 fr run 35
我只对类别的某些元素感兴趣:
ctr = ['ita','fr']
sprt= ['run','golf']
我希望通过这样的方式来提取它们:
df[(df['country']== x for x in ctr)&(df['sport']== x for x in sprt)]
但是虽然它不会抛出任何错误,但它返回空..
有什么建议吗? 我也尝试过:
df[(df['country']== {x for x in ctr})&(df['sport']== {x for x in sprt})]
编辑:
之所以要使用循环,是因为我实际上对每个组合的 3 个最高分感兴趣,我希望将其连接起来:
df1 = pd.concat(df[(df['country']== x for x in ctr)&(df['sport']== x for x in sprt)].sort_values(by=['score'],ascending=False).head(3))
最佳答案
使用双 Series.isin
检查成员(member)资格:
df1 = df[(df['country'].isin(ctr))&(df['sport'].isin(sprt))]
print (df1)
country sport score
1 fr run 25
3 ita run 17
4 fr golf 58
5 fr run 35
df2 = df1.sort_values('score', ascending=False).groupby(['country','sport']).head(3)
print (df2)
country sport score
4 fr golf 58
5 fr run 35
1 fr run 25
3 ita run 17
关于python - 使用循环查询 pandas 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55155926/