对于下面的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
中,并且如果Status
是Make
的Grouped
,我将尝试使用Make_MD3
更新所有相同Make
的Type
列。但是如果Status
不是Grouped
,对于每个Models
,Type
应保持原样。如果“Make_MD3”不存在,则Type
应保持为Make
。
例如,Tesla
不是Grouped
,所以每个模型的Type
都保持不变,但是Jeep
是Grouped
,而Compass
的Type
是Jeep_MD3
,因此,对于所有Jeep
Models
,Jeep_MD3
被更新为Type
。Ford
是Grouped
,但是没有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)
1条答案
按热度按时间t3irkdon1#
请尝试:
印刷品: