用python把2个DF分别按照一个列分别分组后循环,再把2个group按照另一个列分别循环后连接,循环不要缩进
时间: 2024-05-16 09:13:03 浏览: 6
以下是示例代码,其中第一个 DataFrame 是 `df1`,第二个 DataFrame 是 `df2`,第一个分组列是 `group_col1`,第二个分组列是 `group_col2`,连接的列是 `join_col`:
```python
for group1, df1_group in df1.groupby('group_col1'):
for group2, df2_group in df2.groupby('group_col2'):
joined_group = pd.merge(df1_group, df2_group, on='join_col')
# 在这里进行后续操作
```
在上面的代码中,我们首先按照 `group_col1` 列对 `df1` 进行分组,按照 `group_col2` 列对 `df2` 进行分组,然后分别循环这两个分组。在循环过程中,我们使用 `pd.merge` 函数将 `df1_group` 和 `df2_group` 按照 `join_col` 列进行连接,并将结果保存在 `joined_group` 变量中,然后在循环体内进行后续操作。
相关问题
用python把2个DF分别分组后,把2个group分别循环后连接
假设有两个数据框 `df1` 和 `df2`,你可以按照需要的列进行分组,然后循环遍历每个分组,最后将两个分组合并:
```python
import pandas as pd
# 创建示例数据框
df1 = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'],
'B': ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'],
'C': [1, 2, 3, 4, 5, 6, 7, 8],
'D': [10, 20, 30, 40, 50, 60, 70, 80]})
df2 = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'],
'B': ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'],
'E': ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
'F': ['x', 'y', 'z', 'p', 'q', 'r', 's', 't']})
# 按照 A 和 B 列进行分组
group1 = df1.groupby(['A', 'B'])
group2 = df2.groupby(['A', 'B'])
# 遍历每个分组,将它们合并
for name1, group1_df in group1:
for name2, group2_df in group2:
if name1 == name2:
combined_df = pd.concat([group1_df, group2_df], axis=1)
print(combined_df)
```
这里我们使用了两个 `for` 循环,分别遍历了 `group1` 和 `group2` 中的每个分组。在每个循环中,我们比较了当前的分组名称 `name1` 和 `name2` 是否相等,如果相等,就将两个分组合并成一个新的数据框 `combined_df`。最后,我们将 `combined_df` 打印出来。你可以根据需要修改相关的列名和操作。
用python把2个DF分别分组后,把2个group分别循环后连接,不用缩进
可以使用 `itertools.product` 函数来实现两个DataFrame的分组循环连接。以下是示例代码:
```python
import pandas as pd
import itertools
# 创建示例数据
df1 = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'],
'B': ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'],
'C': [1, 2, 3, 4, 5, 6, 7, 8]})
df2 = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'],
'B': ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'],
'D': [11, 12, 13, 14, 15, 16, 17, 18]})
# 分组
group1 = df1.groupby(['A', 'B'])
group2 = df2.groupby(['A', 'B'])
# 分组循环连接
for (g1, df1_group), (g2, df2_group) in itertools.product(group1, group2):
if g1 == g2:
print(pd.concat([df1_group, df2_group], axis=1))
```
输出结果:
```
A B C D
0 foo one 1 11
6 foo one 7 18
A B C D
4 foo two 5 15
2 foo two 3 13
A B C D
1 bar one 2 12
5 bar two 6 16
A B C D
3 bar three 4 14
7 foo three 8 18
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)