简体   繁体   English

将函数应用于基于列的数据帧和基于索引的其他数据帧

[英]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.

相关问题 根据其他列中的值,在列中 dataframe 行的子集上应用 function - apply function on subset of dataframe rows in column based on value in other column Pandas数据帧根据其他列值将函数应用于列字符串 - Pandas dataframe apply function to column strings based on other column value 根据其他列的值组合将函数应用于数据框列 - Apply function to dataframe column based on combinations of values from other columns 根据同一行的其他列中的值将函数应用于dataframe列元素? - Apply function to dataframe column element based on value in other column for same row? 根据其他 dataframe 的列填充一个 dataframe 列 - Populate a dataframe column based on a column of other dataframe apply()基于条件的数据框上的函数 - apply() a function on dataframe based on condition Python dataframe 根据其他id列创建索引列 - Python dataframe create index column based on other id column 根据来自其他列的值使用将 function 应用于多个列,在 dataframe 中创建新列 - Create new column into dataframe based on values from other columns using apply function onto multiple columns 根据日期列索引 dataframe - Index a dataframe based on a date column 将函数应用于pandas Dataframe,其返回值基于其他行 - apply a function to a pandas Dataframe whose returned value is based on other rows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM