3 回答
TA贡献1845条经验 获得超8个赞
IIUC,您可以使用apply()和replace():
df['Product Name'] = df.apply(lambda x: x['Product Name'].replace(x['Size Name'], '').strip(), axis=1)
产量:
ID Product Name Size ID Size Name
0 1 24 Mantra Ancient Grains Foxtail Millet 1 500 gm
1 2 24 Mantra Ancient Grains Little Millet 2 500 gm
2 3 24 Mantra Naturals Almonds 3 100 gm
3 4 24 Mantra Naturals Kismis 4 100 gm
4 5 24 Mantra Organic Ajwain 5 100 gm
5 6 24 Mantra Organic Apple Blast Drink 6 250 ml
6 7 24 Mantra Organic Apple Juice 1 Ltr Tetra Pack 7 1000 ml
7 8 24 Mantra Organic Apple Juice 8 200 ml
8 9 24 Mantra Organic Assam Tea 9 100 gm
TA贡献1831条经验 获得超10个赞
假设您想用 None 替换 Size Name 值,如果它们是 Product Name 的子字符串
df = pd.DataFrame({
'Product Name' : ['24 Mantra Ancient Grains Foxtail Millet 500 gm', '24 Mantra Ancient Grains Little Millet 500 gm ', '24 Mantra Naturals Kismis 100 gm'],
'Size ID' : [1, 2, 3],
'Size Name': ['500 gm', '500 gm', '200 gm']
})
df['same']= df.apply(lambda x: x['Size Name'] in x['Product Name'], axis = 1)
df['Size Name'] = np.where(df['same'], None, df['Size Name'])
df.drop(columns=['same'], inplace = True)
df
Product Name Size ID Size Name
0 24 Mantra Ancient Grains Foxtail Millet 500 gm 1 None
1 24 Mantra Ancient Grains Little Millet 500 gm 2 None
2 24 Mantra Naturals Kismis 100 gm 3 200 gm
添加回答
举报