请帮助。 有数据框:
ArtNo Description Price
AAA Lore Ipsum 10
BBB Lore Ipsum 9
CCC Lore Ipsum 8
DDD AAA Lore Ipsum 0
EEE BBB Lore Ipsum 0
FFF CCC Lore Ipsum 0
GGG ZZZ Lore Ipsum 0
HHH AAA Lore Ipsum 0
我需要在ArtNo列中的Description列中的文章中添加价格乘以而不是零的列。如果不匹配(ArtNo列与Description的第一个单词之间的空格),则将同一行中Price列中的同一图应用于“乘法列”:
ArtNo Description Price Price (multiplied) ???
AAA Lore Ipsum 10 10
BBB Lore Ipsum 9 9
CCC Lore Ipsum 8 8
DDD AAA Lore Ipsum 0 10
EEE BBB Lore Ipsum 0 9
FFF CCC Lore Ipsum 0 8
GGG ZZZ Lore Ipsum 0 0
HHH AAA Lore Ipsum 0 10
在excel中,它的工作方式如下:
IF (Price != 0; Price multiplied = Price;
IF(ISERROR(VLOOKUP(MID(Description;1;FIND(
' ';Description;1));TABLE;3;0));Price multiplied = Price;
ESLE: Price multiplied = VLOOKUP(MID(Description;1;FIND(
' ';Description;1));TABLE;3;0)
)
)
谢谢。
答案 0 :(得分:6)
您可以构建序列映射并将其应用于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
答案 1 :(得分:1)
您可以使用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)