2 回答
TA贡献1828条经验 获得超3个赞
您可以使用np.where来决定在哪里添加数字后缀:
df['purchaseType'] = df.groupby((df['purchaseType']=='FIRST').cumsum())['purchaseType'].transform(
lambda x: np.where(x=='DELIVERY', x+np.arange(len(x)).astype(str), x)
)
print(df)
印刷:
Region Client purchaseType price
0 NY A FIRST 10
1 NY A DELIVERY1 20
2 NY A DELIVERY2 30
3 NY A LAST 25
4 NY B FIRST 15
5 NY B DELIVERY1 10
6 NY B LAST 20
7 FL A FIRST 15
8 FL A DELIVERY1 10
9 NY A DELIVERY2 12
10 NY A DELIVERY3 25
11 NY A LAST 20
TA贡献1828条经验 获得超6个赞
我们可以尝试使用GroupBy.cumcount
andSeries.str.cat
blocks = df['purchaseType'].eq('FIRST').cumsum()
fill_values = df['purchaseType'].str.cat(df.groupby(blocks)
.cumcount().astype(str),
sep='')
df.loc[df['purchaseType'].eq('DELIVERY'), 'purchaseType'] = fill_values
print(df)
# Region Client purchaseType price
# 0 NY A FIRST 10
# 1 NY A DELIVERY1 20
# 2 NY A DELIVERY2 30
# 3 NY A LAST 25
# 4 NY B FIRST 15
# 5 NY B DELIVERY1 10
# 6 NY B LAST 20
# 7 FL A FIRST 15
# 8 FL A DELIVERY1 10
# 9 NY A DELIVERY2 12
# 10 NY A DELIVERY3 25
# 11 NY A LAST 20
添加回答
举报