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

4
  • 2
    For Pandas related questions it helps everyone if you can post a reproducible example of your data rather than a picture of some data. Commented Nov 24, 2019 at 12:56
  • @DavidBuck not only for pandas related question... Commented Nov 24, 2019 at 13:08
  • Are you going to update only by 'UAE' location? Commented Nov 24, 2019 at 13:53
  • Yes I want to update only by 'UAE' Location Commented Nov 24, 2019 at 14:04

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.