简体   繁体   English

groupby + apply + lambda 无法调用我的 function

[英]groupby + apply + lambda fail to call my function

I wrote a function that should normalise values belonging to same week (dividing by the value of the first day):我写了一个 function 应该标准化属于同一周的值(除以第一天的值):

def normalize(week):
        norm_week = (week / week[0]) -1
        return norm_week

I get the week data from a groupby call and I pass it to the normalise function through the apply method (with lambda):我从 groupby 调用中获取周数据,并通过 apply 方法(使用 lambda)将其传递给规范化 function:

dataset['col_1_norm'] = 
dataset.groupby('week_number')['col_1'].apply(lambda x: normalize(x)) 

This is input dataset:这是输入数据集:

week_number   col_1 
week_1.       300 
week_1        500
.....         ... 
week_2        350
.....         ...

I expect the normalised values in the column "col_1_norm", but python returns multiple errors.我希望“col_1_norm”列中的标准化值,但 python 返回多个错误。 (example -> 3361 return self._engine.get_loc(casted_key)) (示例 -> 3361 返回 self._engine.get_loc(casted_key))

Where am I wrong??我哪里错了?? Could you pls help?你能帮忙吗? Thanks Charlie谢谢查理

I inserted a print statement in your normalize routine to debug:我在您的normalize例程中插入了一条打印语句来调试:

def normalize(week):                                                            
    print(week)                                                                 
    norm_week = (week/week[0]) -1                                          
    return norm_week 

Here's what printed out:这是打印出来的:


0      300                                                                                               
1      500                                                                                               
2      900                                                                                               
3     1300                                                                                               
4      200                                                                                               
5      400                                                                                               
6      100                                                                                               
7      800                                                                                               
8      500                                                                                               
9      600                                                                                               
10     500                                                                                               
Name: week_1, dtype: int64                                                                               
11     800                                                                                               
12    1700                
13    1800                                                                                               
14    2000                                                                                               
15    1500                                          
Name: week_2, dtype: int64                                                                               
Traceback (most recent call last):
...

There's no index 0 in the week_2 group. week_2 组中没有索引 0。 Perhaps you meant to use iloc ?也许您打算使用iloc

def normalize(week):                                                            
    norm_week = (week/week.iloc[0]) -1                                          
    return norm_week 

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

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