I have a column of costs in a pandas dataframe. If the cost is more than 10, the percentage we get from that sale is 20%. If it is less than 10, then the percentage we get is 15%. How can I calculate the total revenue (from these percentages we got) for the whole column?
cost_of_order
- 4$
- 7$
- 15$
- 20$
- 3$
Output must be 0.6 1.05 3 4 0.45 = 9.1
CodePudding user response:
you could use the map function to apply a short lambda function for the conditionals.
#this creates the percentage column, not sure if you want this
df['percentage'] = df['cost_of_order'].map(lambda x: x*0.15 if x < 10 else x*0.2)
#this returns the 9.1 as you wanted.
df['percentage'].sum()
CodePudding user response:
You can use pd.cut
:
# Setup: df = pd.DataFrame({'cost_of_order': ['4$', '7$', '15$', '20$', '3$']})
# If needed, remove '$' then convert to numeric
df['cost_of_order'] = pd.to_numeric(df['cost_of_order'].str.rstrip('$'))
pct = pd.cut(df['cost_of_order'].tolist(),
bins=[0, 10, 20, np.nan],
labels=['0.15', '0.2', '0.3']).astype(float)
out = df['cost_of_order'].mul(pct).sum()
Output:
>>> out
9.1