I have 2 dateframes DF1 & DF2.i want perform the division for all DF2_sale column values with DF1_Expectation and update in DF1_percentage
df1 = pd.DataFrame({'Period' : ['Jan', 'Feb', 'Mar'],
'Sale': [10 , 20, 30],
})
df2 = pd.DataFrame({'Loc': ['UAE'],
'Expectation': [98],
})
To apply some operation along an axis of the DataFrame you can always use apply
. For example:
df1['Percentage'] = df1['Sale'].apply(lambda x: x / df2['Expectation'])
or, if instead of a simple division you want to count percentage:
df1['Percentage'] = df1['Sale'].apply(lambda x: x * df2['Expectation'] / 100)
Details in the documentation .
You can use pandas.apply method:
import pandas as pd
df1 = pd.DataFrame({"Period": ["Jan", "Feb", "Mar"], "Sale": [10, 20, 30]})
df2 = pd.DataFrame({"Loc": ["UAE"], "Expectation": [98]})
df1['Percentage'] = df1['Sale'].apply(lambda x: x * df2['Expectation'] / 100)
print(f"df1 = {df1}")
output:
df1 = Period Sale Percentage
0 Jan 10 9.8
1 Feb 20 19.6
2 Mar 30 29.4
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.