python - 行中的过滤器值包含用于进一步计算的特定字符串
问题描述
这是我的数据框:
In [2]: fruits = pd.DataFrame({"apple_price": [100, 100, 200, 500, 100, 600, 200],
...: "cherry_price": [2, 3, 1, 0, 2, 1, 1],
...: "banana_price": [2, 4, 5, 2, 3, 5, 3],
...: "prices": ["apple_price", "apple_price", "cherry_price", "banana_price", "cherry_price",
...: "banana_price", "apple_price"],
...: "price_fruits": [100, 100, 100, 2, 3, 5, 200]})
In [3]: fruits
Out[3]:
apple_price cherry_price banana_price prices price_fruits
0 100 2 2 apple_price 100
1 100 3 4 apple_price 100
2 200 1 5 cherry_price 100
3 500 0 2 banana_price 2
4 100 2 3 cherry_price 3
5 600 1 5 banana_price 5
6 200 1 3 apple_price 200
基本上苹果的价格["apple_price"]
必须除以 100(因为价格apple_price
是美分而不是欧元),而其他水果的价格应该保持不变。
所以,这将是我的预期输出:
In [5]: fruits_ad
Out[5]:
apple_price cherry_price banana_price prices price_fruits pr_fruits_adjusted
0 100 2 2 apple_price 100 1
1 100 3 4 apple_price 100 1
2 200 1 5 cherry_price 100 1
3 500 0 2 banana_price 2 2
4 100 2 3 cherry_price 3 3
5 600 1 5 banana_price 5 5
6 200 1 3 apple_price 200 2
解决方案
根据条件对价格进行调整可以这样完成:
fruits['pr_fruits_adjusted'] = np.where((fruits.price_fruits >=100), fruits['price_fruits']/100, fruits['price_fruits']).astype('int32')
您可以根据您的要求更改条件。在这里,我写了一个条件,如果价格大于或等于 100,则应用调整。
fruits.price_fruits >=100
推荐阅读
- powerbi - 在 DAX 中使用 IF CONTAINS 语句的正确方法是什么?
- crystal-reports - Crystal Reports 交叉表汇总数据子集的行数
- python - 如何在 Google Cloud Datalab 中使用 Python 3.8?
- spring - 无法在 Jenkins 共享库中使用 org.springframework.web.util.UriComponentsBuilder 版本 5.2.13.RELEASE
- docker - Docker 容器离线,即使重启策略总是
- css - 背景过滤器:不透明度()似乎不起作用
- r - 具有按其类标签分类的数据点的热图
- javascript - Joi:如何验证 2 个数组完全不同
- ssis - 通过从 excel 获取输入来创建一个 SSIS 包以运行一些存储过程
- flutter - 从 flutter app 发布数据时出现此错误。未处理的异常:FormatException:无效的 radix-10 数字(在字符 1)