Iterating over multiple groupbys

I want to loop over 4 dataframes, groupby by the same index keys.
The dataframes can contain multiple rows for each index key.

Below is what ChatGPT suggests but I want something better.

I’ve considered concatenating the dataframes, but am concerned about memory usage.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import pandas as pd
import numpy as np
# Example DataFrames
np.random.seed(0)
df1 = pd.DataFrame(np.random.randn(6, 3), index=pd.MultiIndex.from_tuples([(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)], names=['a', 'b']), columns=['A', 'B', 'C'])
df2 = pd.DataFrame(np.random.randn(6, 3), index=pd.MultiIndex.from_tuples([(0, 0), (0, 1), (0, 2), (1, 1), (1, 2), (2, 0)], names=['a', 'b']), columns=['D', 'E', 'F'])
df3 = pd.DataFrame(np.random.randn(5, 3), index=pd.MultiIndex.from_tuples([(0, 0), (0, 2), (1, 0), (1, 2), (2, 1)], names=['a', 'b']), columns=['G', 'H', 'I'])
df4 = pd.DataFrame(np.random.randn(4, 3), index=pd.MultiIndex.from_tuples([(0, 1), (0, 2), (1, 0), (2, 2)], names=['a', 'b']), columns=['J', 'K', 'L'])
# Find common keys
common_keys = set(df1.index).intersection(df2.index).intersection(df3.index).intersection(df4.index)
# Group by multi-level index
grouped1 = df1.groupby(level=['a', 'b'])
grouped2 = df2.groupby(level=['a', 'b'])
grouped3 = df3.groupby(level=['a', 'b'])
grouped4 = df4.groupby(level=['a', 'b'])
# Iterate over the common groups
for key in common_keys:
group1 = grouped1.get_group(key) if key in grouped1.groups else None
group2 = grouped2.get_group(key) if key in grouped2.groups else None
group3 = grouped3.get_group(key) if key in grouped3.groups else None
group4 = grouped4.get_group(key) if key in grouped4.groups else None
print(f"Group {key}:")
print("Group 1:")
print(group1)
print("Group 2:")
print(group2)
print("Group 3:")
print(group3)
print("Group 4:")
print(group4)
print("n")
</code>
<code>import pandas as pd import numpy as np # Example DataFrames np.random.seed(0) df1 = pd.DataFrame(np.random.randn(6, 3), index=pd.MultiIndex.from_tuples([(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)], names=['a', 'b']), columns=['A', 'B', 'C']) df2 = pd.DataFrame(np.random.randn(6, 3), index=pd.MultiIndex.from_tuples([(0, 0), (0, 1), (0, 2), (1, 1), (1, 2), (2, 0)], names=['a', 'b']), columns=['D', 'E', 'F']) df3 = pd.DataFrame(np.random.randn(5, 3), index=pd.MultiIndex.from_tuples([(0, 0), (0, 2), (1, 0), (1, 2), (2, 1)], names=['a', 'b']), columns=['G', 'H', 'I']) df4 = pd.DataFrame(np.random.randn(4, 3), index=pd.MultiIndex.from_tuples([(0, 1), (0, 2), (1, 0), (2, 2)], names=['a', 'b']), columns=['J', 'K', 'L']) # Find common keys common_keys = set(df1.index).intersection(df2.index).intersection(df3.index).intersection(df4.index) # Group by multi-level index grouped1 = df1.groupby(level=['a', 'b']) grouped2 = df2.groupby(level=['a', 'b']) grouped3 = df3.groupby(level=['a', 'b']) grouped4 = df4.groupby(level=['a', 'b']) # Iterate over the common groups for key in common_keys: group1 = grouped1.get_group(key) if key in grouped1.groups else None group2 = grouped2.get_group(key) if key in grouped2.groups else None group3 = grouped3.get_group(key) if key in grouped3.groups else None group4 = grouped4.get_group(key) if key in grouped4.groups else None print(f"Group {key}:") print("Group 1:") print(group1) print("Group 2:") print(group2) print("Group 3:") print(group3) print("Group 4:") print(group4) print("n") </code>
import pandas as pd
import numpy as np

# Example DataFrames
np.random.seed(0)
df1 = pd.DataFrame(np.random.randn(6, 3), index=pd.MultiIndex.from_tuples([(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)], names=['a', 'b']), columns=['A', 'B', 'C'])
df2 = pd.DataFrame(np.random.randn(6, 3), index=pd.MultiIndex.from_tuples([(0, 0), (0, 1), (0, 2), (1, 1), (1, 2), (2, 0)], names=['a', 'b']), columns=['D', 'E', 'F'])
df3 = pd.DataFrame(np.random.randn(5, 3), index=pd.MultiIndex.from_tuples([(0, 0), (0, 2), (1, 0), (1, 2), (2, 1)], names=['a', 'b']), columns=['G', 'H', 'I'])
df4 = pd.DataFrame(np.random.randn(4, 3), index=pd.MultiIndex.from_tuples([(0, 1), (0, 2), (1, 0), (2, 2)], names=['a', 'b']), columns=['J', 'K', 'L'])

# Find common keys
common_keys = set(df1.index).intersection(df2.index).intersection(df3.index).intersection(df4.index)

# Group by multi-level index
grouped1 = df1.groupby(level=['a', 'b'])
grouped2 = df2.groupby(level=['a', 'b'])
grouped3 = df3.groupby(level=['a', 'b'])
grouped4 = df4.groupby(level=['a', 'b'])

# Iterate over the common groups
for key in common_keys:
    group1 = grouped1.get_group(key) if key in grouped1.groups else None
    group2 = grouped2.get_group(key) if key in grouped2.groups else None
    group3 = grouped3.get_group(key) if key in grouped3.groups else None
    group4 = grouped4.get_group(key) if key in grouped4.groups else None
    
    print(f"Group {key}:")
    print("Group 1:")
    print(group1)
    print("Group 2:")
    print(group2)
    print("Group 3:")
    print(group3)
    print("Group 4:")
    print(group4)
    print("n")

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật