这是我的数据帧:
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
基本上,苹果的价格必须除以100(因为苹果的价格是美分而不是欧元),而其他水果的价格应该保持不变
这就是我的预期输出:
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
根据条件对价格进行调整可以这样做:
您可以根据需要更改条件。这里我写了一个条件,如果价格大于或等于100,则进行调整。
fruits.price_fruits >=100
抱歉,伙计们,我太匆忙了,把我的示例数据框搞砸了
你看,伙计:
你从这里开始
这应该是预期的输出:
简而言之,我要查找的代码将字符串拆分为“Variance”列,并检查我是否包含“apple”,如果此条件==True,则应将apple_价格除以100,否则从“price”列中获取相应水果的价格
这是我的尝试也许你有更优雅的东西:
干杯 类风湿关节炎
相关问题 更多 >
编程相关推荐