简体   繁体   English

根据行值将函数应用于Pandas中的列

[英]Apply function to column in Pandas depending on row value

Let's say I have the following dataframe: 假设我有以下数据框:

date,id,value
1/1/2017,5,300
1/1/2017,51,300
1/1/2017,54,300
1/2/2017,5,100
1/2/2017,51,100
1/2/2017,54,100

and I have a dictionary mapping id to an adjustment factor as such: 而且我有一个字典映射id到这样的调整因子:

{5: 20, 51: 23.5, 54:10}

I want to add the factor corresponding to the id to the value column in my dataframe, resulting in: 我想addid对应的因数add到数据框中的value列,导致:

date,id,value,adjusted_value
1/1/2017,5,300,300+20=320
1/1/2017,51,310,310+23.5=333.5
1/1/2017,54,320,320+10=330
1/2/2017,5,110,110+20=130
1/2/2017,51,120,120+23.5=143.5
1/2/2017,54,130,130+10=140

Is there a simple way to do this? 有没有简单的方法可以做到这一点?

I think you are looking for ngroup, cumcount and mapping ie 我想你正在寻找ngroup,cumcount和map即

x = df.groupby('date')
d = {5: 20, 51: 23.5, 54: 10}
df['new'] = (x.cumcount()+x.ngroup())*10 +df['id'].map(d)+df['value']

Output : 输出:

date  id  value    new
0  1/1/2017   5    300  320.0
1  1/1/2017  51    300  333.5
2  1/1/2017  54    300  330.0
3  1/2/2017   5    100  130.0
4  1/2/2017  51    100  143.5
5  1/2/2017  54    100  140.0

Explanation 说明

(x.cumcount()+x.ngroup()
0    0
1    1
2    2
3    1
4    2
5    3
 (x.cumcount()+x.ngroup())*10 +df['value'] 
0    300
1    310
2    320
3    110
4    120
5    130
dtype: int64

One-liner: 一内胆:

df['adjusted_value'] = df.apply(lambda x: dictionary[x['id']] + x['value'] , axis=1)

More verbose: 更详细:

df['adjusted_value'] = [dictionary[i] for i in df['id']]
df['adjusted_value'] = df['adjusted_value'] + df['value']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM