I have one dataframe and one series, the dataframe is the base data about the cost for each respondents, and series is discount rate that should be multiplied to the base data.
For example, base data is (dataframe):
cost1 cost2
John 100 50
Tom 50 100
Andy 50 200
Mark 80 300
and discount data is (series):
John 0.7
Tom 0.6
Andy 0.9
Mark 0.5
The desired output is therefore:
cost1 cost2
John 70 35
Tom 30 60
Andy 45 180
Mark 40 150
The method I have come up with is:
customer_list = base.index.tolist()
for k in customer_list:
base.loc[k] = base.loc[k]*discount.loc[k]
This works (gives me the desired output), but I feel it's quite inefficient. In my actual application, the dataframe is quite large, so it takes quite time to run such iteration.
Any suggestions for the efficient calculation? I think there might be some efficient way to do such calculation as a whole, using the index. (base * discount certainly did not work though...)
thank you in advance!
Use mul
with an axis
param:
df.mul(df2, axis=0)
cost1 cost2
John 70.0 35.0
Tom 30.0 60.0
Andy 45.0 180.0
Mark 40.0 150.0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.