pandas - 循环遍历 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%。
我应该如何进行?
解决方案
使用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
推荐阅读
- r - 为什么在 Windows 上通过 Powershell 将数据加载到 Docker 中的 clickhouse 时出现换行错误?
- python - ValueError:合并键在左侧 Pandas 上包含空值
- imagemagick - WebP 的 SSIM 计算
- jquery - onclick 未捕获的语法错误?
- android-layout - 将拉伸的行放在颤振应用程序的列中
- ruby-on-rails - Heroku Rails 部署/构建错误:找不到 Gemfile.lock 所需的“捆绑程序”(版本)
- algorithm - D* Lite:你能根据实际机器人位置更改起始节点吗?
- django - 如何在 django 中的输入标签之后添加跨度而不手动呈现表单?
- sql - PostgreSQL - 检查列是否存在并嵌套条件语句
- php - Laravel 对象属性在循环时为空,但在 json_encode() 时具有值