Reputation: 423
I have the following pandas DataFrame and I'm trying to do a little clean up. In my case, I'm receiving product 'a' data in raw decimal form whereas I need it to be percent to align with the formatting for the other products.
How do I scale the success_rate and market_penetration_rate by 100 in my dataframe only in cases when product = a?
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 | +----------+--------------+-------------------------+
Upvotes: 4
Views: 10622
Reputation: 6383
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
Or if you want to scale in-place,
In [8]: df.loc[df['product']=='a', ['market_penetration_rate', 'success_rate']] *= 100
Upvotes: 16
Reputation: 3086
try this:
df.apply(lambda row: row[['success_rate', 'market_penetration_rate']]*100 if row['product'] == 'a'
else row[['success_rate', 'market_penetration_rate']], axis=1)
Upvotes: 1