我可以合并分组数据吗? [英] Can I combine groupby data?
问题描述
我有两列主场和客场.因此,一排将是英格兰对阵巴西,而下一排将是巴西英格兰.我怎样才能一次统计巴西面对英格兰或英格兰对巴西的次数?
I have two columns home and away. So one row will be England vs Brazil and the next row will be Brazil England. How can I count occurrences of when Brazil faces England or England vs Brazil in one count?
基于以前的解决方案,我已经尝试过
Based on previous solutions, I have tried
results.groupby(["home_team", "away_team"]).size()
results.groupby(["away_team", "home_team"]).size()
然而,这并没有给我我正在寻找的结果.
however this does not give me the outcome that I am looking for.
不需要的输出:home_team 客场_team
英格兰 巴西 1
Undesired output:
home_team away_team
England Brazil 1
away_team home_team
巴西英格兰1
away_team home_team
Brazil England 1
我想看:英格兰巴西2
推荐答案
您可以通过 numpy.sort
,创建 DataFrame
并使用您的原始解决方案:
You can sort values by numpy.sort
, create DataFrame
and use your original solution:
df1 = (pd.DataFrame(np.sort(df[['home','away']], axis=1), columns=['home','away'])
.groupby(["home", "away"])
.size())
这篇关于我可以合并分组数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!