2 回答

TA贡献1860条经验 获得超8个赞
您可以构建一个系列映射并将其应用于Description.
zeros = df['Price'].eq(0)
art_price_map = df[~zeros].set_index('ArtNo')['Price']
first_word = df['Description'].str.split(n=1).str[0]
df['Price (multiplied)'] = df['Price'].mask(zeros, first_word.map(art_price_map))\
.fillna(0).astype(int)
print(df)
ArtNo Description Price Price (multiplied)
0 AAA Lore Ipsum 10 10
1 BBB Lore Ipsum 9 9
2 CCC Lore Ipsum 8 8
3 DDD AAA Lore Ipsum 0 10
4 EEE BBB Lore Ipsum 0 9
5 FFF CCC Lore Ipsum 0 8
6 GGG ZZZ Lore Ipsum 0 0

TA贡献1877条经验 获得超6个赞
你可以这样做pd.merge:
#create new dataframe with ArtNo created from part of the Description
df2 = df.copy()[['Description']]
df2.columns = ['ArtNo']
df2['ArtNo'] = df2['ArtNo'].str.split(n=1).str[0]
#merge price from the first dataframe
df2 = pd.merge(df2, df[['ArtNo', 'Price']], how='left', on='ArtNo')
#create a new column 'Price (multiplied)' and fill NANs from original 'Price' column
df['Price (multiplied)'] = df2['Price'].values
df['Price (multiplied)'] = df['Price (multiplied)'].fillna(df['Price']).astype(int)
添加回答
举报