Peterhack
Peterhack

Reputation: 1061

Apply function to dataframe based on column with other dataframe based on index

I would like to perform some operation (e.g. x*apples^y) on the values of column apples, based on their color. The corresponding values are in a seperate dataframe:

import pandas as pd
import numpy as np
df1 = pd.DataFrame({'apples': [2, 1, 5, 6, 7], 'color': [1, 1, 1, 2, 2]})
df2 = pd.DataFrame({'x': [100, 200], 'y': [0.5, 0.3]}).set_index(np.array([1, 2]), 'color')

I am looking for the following result:

   apples        color
0  100*2^0.5      1
1  100*1^0.5      1
2  100*5^0.5      1
3  200*6^0.3      2
4  200*7^0.3      2

Upvotes: 1

Views: 65

Answers (2)

Vivek Kalyanarangan
Vivek Kalyanarangan

Reputation: 9081

I think you need pandas.merge -

temp = df1.merge(df2, left_on='color', right_index= True, how='left')
df1['apples'] = (temp['x']*(temp['apples'].pow(temp['y'])))

Output

       apples  color
0  141.421356      1
1  100.000000      1
2  223.606798      1
3  342.353972      2
4  358.557993      2

Upvotes: 0

jezrael
jezrael

Reputation: 863166

Use DataFrame.join with default left join first and then operate with appended columns:

df = df1.join(df2, on='color')
df['apples'] = df['x'] * df['apples'] ** df['y']
print (df)
       apples  color    x    y
0  141.421356      1  100  0.5
1  100.000000      1  100  0.5
2  223.606798      1  100  0.5
3  342.353972      2  200  0.3
4  358.557993      2  200  0.3

There is left join, so append to new column in df1 should working:

df = df1.join(df2, on='color')
df1['apples'] = df['x'] * df['apples'] ** df['y']
print (df1)
       apples  color
0  141.421356      1
1  100.000000      1
2  223.606798      1
3  342.353972      2
4  358.557993      2

Another idea is use double map:

df1['apples'] = df1['color'].map(df2['x']) * df1['apples'] ** df1['color'].map(df2['y'])
print (df1)
       apples  color
0  141.421356      1
1  100.000000      1
2  223.606798      1
3  342.353972      2
4  358.557993      2

Upvotes: 1

Related Questions