[英]Apply function to dataframe based on column with other dataframe based on index
I would like to perform some operation (eg x*apples^y
) on the values of column apples, based on their color.我想根据它们的颜色对列苹果的值执行一些操作(例如
x*apples^y
)。 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
Use DataFrame.join
with default left join first and then operate with appended columns:首先将
DataFrame.join
与默认左连接一起使用,然后使用附加列进行操作:
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:有左连接,因此附加到
df1
新列应该可以工作:
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
:另一个想法是使用双
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
I think you need pandas.merge -我认为你需要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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.