为 Pandas 中购买的汽车颜色、宠物和物品的所有迭代连接三个数据框 - groupby?

Joining Three Data Frames for all Iterations of Car Color, Pets, and Items Purchased in Pandas- groupby?

我目前有三个数据框:购买、汽车颜色和宠物。可以使用以下代码形成该数据的示例:

import pandas as pd

cols = ['MEMBER', 'CAR_COLOR', 'PET', 'PURCHASE_TYPE', 'ITEM_1', 'ITEM_2', 'ITEM_3', 'ITEM_4']

data1 = [
     [1, "", "", "CC", "BALL", "SHIRT", "VIOLIN", "SWEATER"],
     [1, "", "", "CC", "CANVAS", "COFFEE", "", ""],
     [1, "", "", "CSH", "TOY", "VIDEO_GAME", "GUITAR", ""],
     [2, "", "", "CC", "VEST", "BOOK", "EGGS", "BREAD"],
     [2, "", "", "CHK", "APPLES", "TOOLS", "", ""]
]

colors = [
    [1,"BLUE"],
    [1, "RED"],
    [2, "BLUE"],
    [2, "GREEN"],
    [2, "WHITE"]
]

pets = [
    [1, 'FISH'],
    [2, 'DOG'],
    [2, 'CAT'],
    [2, 'FISH']
]

df_data = pd.DataFrame(data1, columns=cols)
df_colors = pd.DataFrame(colors, columns=['MEMBER', 'CAR_COLOR'])
df_pets = pd.DataFrame(pets, columns = ['MEMBER', 'PET'])

本质上,我想加入这些数据框,以便
的每个唯一迭代 (MEMBER, PURCHASE_TYPE, ITEMS1-4)/CAR_COLOR/PET 列为它自己的行。最初,购买数据框 (data1) 具有唯一的成对值 (MEMBER, PURCHASE_TYPE),但这些值被缩减,如果一对有四个以上的项目,第 4 项之后的项目将成为它们自己的新行,如 data1 的第 1 行和第 2 行所示。最终的数据框应该看起来像 this image.

在我的实际情况下,三个初始 Dataframes 中的每一个都有数千行,因此理想情况下,解决方案应该是健壮的/易于简化为更大的数据集。如果这不是足够的信息或者您有任何相关问题,请告诉我。我想解决方案涉及某种分组和合并,但由于某种原因我的大脑遇到了这个问题。任何帮助都将不胜感激!

你可以这样做:

cols = ['MEMBER', 'PURCHASE_TYPE', 'ITEM_1', 'ITEM_2', 'ITEM_3', 'ITEM_4']
df_out  = (df_data[cols].set_index('MEMBER').join([di.set_index('MEMBER') for di in [df_colors, df_pets]])
             .sort_values(['MEMBER', 'PET']).reset_index())

输出:

    MEMBER PURCHASE_TYPE  ITEM_1      ITEM_2  ITEM_3   ITEM_4 CAR_COLOR   PET
0        1            CC    BALL       SHIRT  VIOLIN  SWEATER      BLUE  FISH
1        1            CC    BALL       SHIRT  VIOLIN  SWEATER       RED  FISH
2        1            CC  CANVAS      COFFEE                       BLUE  FISH
3        1            CC  CANVAS      COFFEE                        RED  FISH
4        1           CSH     TOY  VIDEO_GAME  GUITAR               BLUE  FISH
5        1           CSH     TOY  VIDEO_GAME  GUITAR                RED  FISH
6        2            CC    VEST        BOOK    EGGS    BREAD      BLUE   CAT
7        2            CC    VEST        BOOK    EGGS    BREAD     GREEN   CAT
8        2            CC    VEST        BOOK    EGGS    BREAD     WHITE   CAT
9        2           CHK  APPLES       TOOLS                       BLUE   CAT
10       2           CHK  APPLES       TOOLS                      GREEN   CAT
11       2           CHK  APPLES       TOOLS                      WHITE   CAT
12       2            CC    VEST        BOOK    EGGS    BREAD      BLUE   DOG
13       2            CC    VEST        BOOK    EGGS    BREAD     GREEN   DOG
14       2            CC    VEST        BOOK    EGGS    BREAD     WHITE   DOG
15       2           CHK  APPLES       TOOLS                       BLUE   DOG
16       2           CHK  APPLES       TOOLS                      GREEN   DOG
17       2           CHK  APPLES       TOOLS                      WHITE   DOG
18       2            CC    VEST        BOOK    EGGS    BREAD      BLUE  FISH
19       2            CC    VEST        BOOK    EGGS    BREAD     GREEN  FISH
20       2            CC    VEST        BOOK    EGGS    BREAD     WHITE  FISH
21       2           CHK  APPLES       TOOLS                       BLUE  FISH
22       2           CHK  APPLES       TOOLS                      GREEN  FISH
23       2           CHK  APPLES       TOOLS                      WHITE  FISH