0

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],
               })

Please refer attached dataframe screens DF1

DF2

tida kiran
  • 5
  • 1
  • 6

2 Answers2

1

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.

Pawel Kam
  • 1,684
  • 3
  • 14
  • 30
0

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  
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179