gpt4 book ai didi

python - Pandas 条件乘法

转载 作者:行者123 更新时间:2023-11-28 20:04:52 25 4
gpt4 key购买 nike

我有以下 pandas DataFrame,我正在尝试做一些清理工作。在我的例子中,我收到了原始十进制形式的产品“a”数据,而我需要它是百分比以与其他产品的格式保持一致。

如何仅在 product = a 的情况下在我的数据框中将 success_ratema​​rket_penetration_rate 缩放 100?

import pandas as pd

df = pd.DataFrame({'product' : ['a', 'a', 'c', 'c', 'd', 'b', 'a', 'b', 'c'],
'success_rate' : [0.2, 1.0, 67.0, 71.5, 23.2, 71.0, 0.44, 59.3, 12.7],
'market_penetration_rate' : [0.82, 0.64, 77.5, 12.5, 22.5, 88.0, 0.34, 98.2, 87.4]})
+----------+--------------+-------------------------+| product  | success_rate | market_penetration_rate ||          |              |                         || a        | 0.2          | 0.82                    ||          |              |                         || a        | 1            | 0.64                    ||          |              |                         || c        | 67           | 77.5                    ||          |              |                         || c        | 71.5         | 12.5                    ||          |              |                         || d        | 23.2         | 22.5                    ||          |              |                         || b        | 71           | 88                      ||          |              |                         || a        | 0.44         | 0.34                    ||          |              |                         || b        | 59.3         | 98.2                    ||          |              |                         || c        | 12.7         | 87.4                    |+----------+--------------+-------------------------+

最佳答案

In [7]: print df.loc[df['product']=='a', ['market_penetration_rate', 'success_rate']] * 100
market_penetration_rate success_rate
0 82 20
1 64 100
6 34 44

或者如果你想就地缩放,

In [8]: df.loc[df['product']=='a', ['market_penetration_rate', 'success_rate']] *= 100

关于python - Pandas 条件乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34803670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com