[英]Filter pandas data frame on unique pair values
I have a pandas data frame that contains information about games between two teams played at a different time: 我有一个熊猫数据框,其中包含有关两支球队在不同时间比赛的信息:
TeamA TeamB Time
1 2 12:06
1 2 13.14
1 3 14.14
1 3 13.14
3 1 15.22
What is the simplest way (not double for loop) to obtain "sub" data frames that contain only the information about unique pairs of teams such as: 什么是获取仅包含有关唯一的成对团队信息的“子”数据帧的最简单方法(不是double for循环),例如:
For (1,2): 对于(1,2):
TeamA TeamB Time
1 2 12:06
1 2 13.14
For (1,3): 对于(1,3):
TeamA TeamB Time
1 3 14.14
1 3 13.14
Etc. 等等。
EDIT : 编辑 :
I don't know in advance the teams that are present in the data frame. 我事先不知道数据框中存在的团队。 That is I'd need a data frame for every possible pair of teams. 那就是我需要每个可能的团队对都有一个数据框。
Create dictionary of DataFrames
: 创建dictionary of DataFrames
:
dfs = dict(tuple(df.groupby(['TeamA','TeamB'])))
print (dfs[(1,2)])
TeamA TeamB Time
0 1 2 12:06
1 1 2 13.14
print (dfs[(1,3)])
TeamA TeamB Time
2 1 3 14.14
3 1 3 13.14
If want all values: 如果需要所有值:
for i, x in dfs.items():
print (x)
TeamA TeamB Time
0 1 2 12:06
1 1 2 13.14
TeamA TeamB Time
2 1 3 14.14
3 1 3 13.14
TeamA TeamB Time
4 3 1 15.22
If want last row in same group: 如果要在同一组中的最后一行:
cols = ['TeamA','TeamB']
a = df[cols].apply(sorted, 1)
dfs = dict(tuple(df.groupby([a[cols[0]], a[cols[1]]])))
for i, x in dfs.items():
print (x)
TeamA TeamB Time
0 1 2 12:06
1 1 2 13.14
TeamA TeamB Time
2 1 3 14.14
3 1 3 13.14
4 3 1 15.22
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.