[英]Apply function to each n rows pandas
I have a pandas df col which looks like the following: 我有一个熊猫df col,看起来像下面的样子:
0 0.286
1 0.240
2 0.335
3 0.397
2430 38.580
2431 38.650
2432 38.630
2433 38.170
6007 72.960
6008 71.250
6009 70.370
6010 70.460 ...
I would like to output a new_col
with the % change from the initial value, resetting every fourth value, then a final 4 line output which takes the average of every fourth value in the new_col
. 我想输出一个从初始值new_col
有%更改的new_col
,重置每个第四个值,然后输出最后的4行输出,该输出采用new_col
中每个第四个值的new_col
。
Expected output new_col
: 预期输出new_col
:
0.00
-16.08
17.13
38.81
0.00
0.18
0.13
-1.06
0.00
-2.34
-3.55
-3.43
avg_col
0.00
-6.08
4.57
11.44
You can get the new_col
by grouping every 4 lines: 您可以通过每4行分组来获得new_col
:
df['new_col'] = df.groupby(df.index//4)[1].apply(lambda x: (x-x.iloc[0])/x.iloc[0]*100).reset_index(0, drop=True)
Or to avoid the .groupby.apply
perhaps transform and then do the calculation (might be faster for large Frames) 或为了避免.groupby.apply
转换然后进行计算(对于大帧,可能会更快)
df['new_col'] = df.groupby(df.index//4)[1].transform('first')
df['new_col'] = (df[1] - df.new_col)/df.new_col*100
df
: 输出df
: 0 1 new_col
0 0 0.286 0.000000
1 1 0.240 -16.083916
2 2 0.335 17.132867
3 3 0.397 38.811189
4 2430 38.580 0.000000
5 2431 38.650 0.181441
6 2432 38.630 0.129601
7 2433 38.170 -1.062727
8 6007 72.960 0.000000
9 6008 71.250 -2.343750
10 6009 70.370 -3.549890
11 6010 70.460 -3.426535
Get the average by grouping by the division remainder: 通过按除法除法器余数分组来获得平均值:
df.groupby(df.index%4).new_col.mean()
0 0.000000
1 -6.082075
2 4.570859
3 11.440642
Name: new_col, dtype: float64
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.