Python 熊猫-罐头盒';I don’我不能让Series.i工作
我有一个比赛结果的数据框,我试图看看比赛的获胜者是否来自与比赛相同的地点 圆形定位柱:Python 熊猫-罐头盒';I don’我不能让Series.i工作,python,pandas,dataframe,series,Python,Pandas,Dataframe,Series,我有一个比赛结果的数据框,我试图看看比赛的获胜者是否来自与比赛相同的地点 圆形定位柱: 0 Val d'Allos, France 168 Les Deux Alpes, France 378 Winter Park, CO, USA 499 Whistler, BC, Canada ... 国家栏: 0 Franc
0 Val d'Allos, France
168 Les Deux Alpes, France
378 Winter Park, CO, USA
499 Whistler, BC, Canada
...
国家栏:
0 France
168 France
378 France
499 Australia
602 France
...
我的代码:
winners_df = df.loc[df['finish_position'] == 1, ['country', 'round_loc']]
hometown_win = winners_df['country'].isin(winners_df['round_loc'])
# Also tried
hometown_win = winners_df['country'].isin(winners_df['round_loc'].values)
print(hometown_win)
我的结果:
0 False
168 False
378 False
499 False
602 False
...
不知道我做错了什么
winners_df['country'][0] in winners_df['round_loc'][0]
很好。我相信我可以通过一个循环来完成,但我觉得我错过了一些东西
print (winners_df)
round_loc country
0 Val d'Allos, France France
168 Les Deux Alpes, France USA <-changed data sample
378 Winter Park, CO, USA France
499 Whistler, BC, Canada Australia
如果需要,检查列round\u loc
中是否有来自列国家的一个值,但每行:
hometown_win = winners_df.apply(lambda x: x['country'] in x['round_loc'],axis=1)
print(hometown_win)
0 True
168 False
378 False
499 False
dtype: bool
这将不起作用。isin
正在寻找精确的匹配项,您需要比较每一行并使用contains
来测试此处的成员资格/匹配项ahhh这是有意义的。如果法国有一场比赛(round_loc)不是由法国(国家)的人赢得的,因为每一排都有一名参赛者,那么这里似乎有一个问题。是的,没错。如果我的回答有帮助,别忘了。谢谢,太好了!非常感谢。我不知道你可以像那样检查轴上的所有项目!
hometown_win = winners_df.apply(lambda x: x['country'] in x['round_loc'],axis=1)
print(hometown_win)
0 True
168 False
378 False
499 False
dtype: bool