首页 > 解决方案 > 循环遍历 Pandas 数据框并添加注释

问题描述

我正在尝试根据不同列中的值向数据框添加注释。这是我的植物销售数据:

     UPC  Quantity       Name    Size   Color       Type
0  11313        68     Crocus   small  purple  perennial
1  42164        51  Sunflower   large  yellow     annual
2  53890        76     Zinnia   small    pink     annual
3  54885        85      Daisy  medium   white  perennial
4  77314        80       Rose   large     red  perennial
5  79375        62        Mum  medium  yellow     annual
6  94281         1    Dahlia    large  orange  perennial
7  95085        41   Marigold   small  orange     annual

import pandas as pd
df = pd.read_csv("Plants_in_stock.csv")
#I add a column to annotate a discount for products I want to move
df['Discount'] = ''
df

到目前为止,这是输出的样子

现在我想附加“折扣”列,如果“数量”> 80,则为 25%,如果“类型”==“年度”,则为 50%。

我应该如何进行?

标签: pandasappend

解决方案


使用np.select

conds = [df.Quantity > 80, df['Type'] == 'annual']

choices = [0.25,0.5]
# or if you prefer strings formated with %:
# choices = ['25%','50%']

df['discount'] = pd.np.select(conds, choices)

>>> df
     UPC  Quantity       Name    Size   Color       Type  discount
0  11313        68     Crocus   small  purple  perennial      0.00
1  42164        51  Sunflower   large  yellow     annual      0.50
2  53890        76     Zinnia   small    pink     annual      0.50
3  54885        85      Daisy  medium   white  perennial      0.25
4  77314        80       Rose   large     red  perennial      0.00
5  79375        62        Mum  medium  yellow     annual      0.50
6  94281         1    Dahlia    large  orange  perennial      0.00
7  95085        41   Marigold   small  orange     annual      0.50

推荐阅读