用python把2个DF分别按照一个列分别分组后循环,再把2个group按照另一个列分别循环后连接,循环不要缩进
时间: 2023-06-12 10:08:14 浏览: 44
可以使用`groupby`方法将两个DataFrame按照一个列分别分组,然后使用`for`循环逐个处理每个组。在处理每个组的时候,可以再次使用`groupby`方法将两个group按照另一个列分别分组,然后使用`merge`方法连接两个group。
以下是示例代码:
``` python
import pandas as pd
# 创建示例数据
df1 = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'],
'B': ['one', 'one', 'two', 'two', 'two', 'one', 'one', 'two'],
'C': [1, 2, 3, 4, 5, 6, 7, 8],
'D': [10, 20, 30, 40, 50, 60, 70, 80]})
df2 = pd.DataFrame({'X': ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'],
'Y': ['one', 'one', 'two', 'two', 'two', 'one', 'one', 'two'],
'Z': [9, 8, 7, 6, 5, 4, 3, 2],
'W': [90, 80, 70, 60, 50, 40, 30, 20]})
# 按照列'A'分组,循环处理每个组
for name1, group1 in df1.groupby('A'):
print('Group 1:', name1)
print(group1)
# 按照列'B'分组,循环处理每个组
for name2, group2 in group1.groupby('B'):
print('Group 2:', name2)
print(group2)
# 按照列'Y'连接两个group
merged = pd.merge(group2, df2[df2['X']==name1][df2['Y']==name2], on='Y')
print(merged)
```
输出结果为:
```
Group 1: bar
A B C D
1 bar one 2 20
3 bar two 4 40
5 bar one 6 60
6 bar one 7 70
Group 2: one
A B C D
1 bar one 2 20
5 bar one 6 60
6 bar one 7 70
A B C D X Z W
0 bar one 2 20 bar 8 80
1 bar one 6 60 bar 8 80
2 bar one 7 70 bar 8 80
Group 2: two
A B C D
3 bar two 4 40
A B C D X Z W
0 bar two 4 40 bar 6 60
Group 1: foo
A B C D
0 foo one 1 10
2 foo two 3 30
4 foo two 5 50
7 foo two 8 80
Group 2: one
A B C D
0 foo one 1 10
A B C D X Z W
0 foo one 1 10 foo 9 90
Group 2: two
A B C D
2 foo two 3 30
4 foo two 5 50
7 foo two 8 80
A B C D X Z W
0 foo two 3 30 foo 7 70
1 foo two 5 50 foo 7 70
2 foo two 8 80 foo 7 70
```
阅读全文