基于Pandas中的多个复杂条件更新列

0sgqnhkj  于 2022-11-05  发布在  其他
关注(0)|答案(1)|浏览(179)

对于下面的DataFrame df

df = pd.DataFrame([('Tesla','Model3', '-', 'Tesla'),
                  ('Tesla', 'ModelS', '-', 'Tesla_MD3'),
                  ('Tesla', 'ModelX', '-', 'Tesla_MD3'),
                  ('Tesla', 'ModelY', '-', 'Tesla'),
                  ('Jeep',  'Wrangler','Grouped','Jeep'),
                  ('Jeep',  'Compass', 'Grouped','Jeep_MD3'),
                  ('Jeep',  'Patriot', 'Grouped','Jeep'),
                  ('Jeep',  'Cherokee','Grouped','Jeep'),
                  ('Ford',  'Mustang', 'Grouped','Ford'),
                  ('Ford',  'F150',     'Grouped','Ford')  ],columns=['Make','Model','Status','Type'])

df
     Make    Model        Status    Type
0   Tesla   Model3             -    Tesla
1   Tesla   ModelS             -    Tesla_MD3
2   Tesla   ModelX             -    Tesla_MD3
3   Tesla   ModelY             -    Tesla
4   Jeep    Wrangler     Grouped    Jeep
5   Jeep    Compass      Grouped    Jeep_MD3
6   Jeep    Patriot      Grouped    Jeep
7   Jeep    Cherokee     Grouped    Jeep
8   Ford    Mustang      Grouped    Ford
9   Ford    F150         Grouped    Engine

如果Make_MD3出现在Make中的任何一个Models中,并且如果StatusMakeGrouped,我将尝试使用Make_MD3更新所有相同MakeType列。但是如果Status不是Grouped,对于每个ModelsType应保持原样。如果“Make_MD3”不存在,则Type应保持为Make
例如,Tesla不是Grouped,所以每个模型的Type都保持不变,但是JeepGrouped,而CompassTypeJeep_MD3,因此,对于所有JeepModelsJeep_MD3被更新为TypeFordGrouped,但是没有Models具有Ford_MD3类型,因此Type被保持为Ford
预期产出

Make    Model    Status       Type
0   Tesla   Model3      -           Tesla  #For Tesla Type is maintained for each model seperately since it is not grouped
1   Tesla   ModelS      -         Tesla_MD3
2   Tesla   ModelX      -         Tesla_MD3
3   Tesla   ModelY      -            Tesla
4   Jeep    Wrangler   Grouped     Jeep_MD3 #Since Jeep is grouped, all its Type is changed to Jeep_MD3 since one of the model had Jeep_MD3
5   Jeep    Compass    Grouped     Jeep_MD3
6   Jeep    Patriot    Grouped     Jeep_MD3
7   Jeep    Cherokee   Grouped     Jeep_MD3
8   Ford    Mustang    Grouped        Ford #Even though Ford is grouped, since there is no Ford_MD3 the Type is maintained as Ford.
9   Ford    F150       Grouped        Ford

换句话说,条件适用于所有makes(如吉普车)如果make被分组,并且如果MD3被附加到任何型号types,那么同一make中的所有分组型号都将有MD3附加到它们。我尝试了下面的代码,但它不工作。有人能修复这种方法或尝试一种新的方法吗?

df.loc[df.Status.eq('Grouped'), 'Type'] = df[df.Status.eq('Grouped')].groupby('Make').Type.transform(
    lambda x: f"{x.str.replace('_MD3','')}_{MD3}" if x.str.contains('_MD3').any() else x)
t3irkdon

t3irkdon1#

请尝试:

df.loc[df.Status == "Grouped", "Type"] = (
    df[df.Status == "Grouped"]
    .groupby("Make")["Type"]
    .transform(
        lambda x: x.name + "_MD3" if x.str.endswith("_MD3").any() else x.name
    )
)

print(df)

印刷品:

Make     Model   Status       Type
0  Tesla    Model3        -      Tesla
1  Tesla    ModelS        -  Tesla_MD3
2  Tesla    ModelX        -  Tesla_MD3
3  Tesla    ModelY        -      Tesla
4   Jeep  Wrangler  Grouped   Jeep_MD3
5   Jeep   Compass  Grouped   Jeep_MD3
6   Jeep   Patriot  Grouped   Jeep_MD3
7   Jeep  Cherokee  Grouped   Jeep_MD3
8   Ford   Mustang  Grouped       Ford
9   Ford      F150  Grouped       Ford

相关问题