python_求每组的差值 import pandas as pd import numpy as np data = [[ 1 , 2 , 3 ],[ 1 , 2 , 6 ],[ 1 , 3 , 5 ]] columns = [ 'id01' , 'id02' , 'test3' ] frame = pd . DataFrame ( data = data , columns = columns ) # frame = pd.DataFrame(np
python_求每组的差值
import pandas as pdimport numpy as np
data = [[1,2,3],[1,2,6],[1,3,5]]
columns=['id01','id02','test3']
frame = pd.DataFrame(data=data, columns=columns)
# frame = pd.DataFrame(np.random.randn(4, 3),columns=columns)
# 排序
frame=frame.sort_values(['id01','id02','test3']).reset_index(drop = True)
frame
id01 id02 test3
0 1 2 3
1 1 2 6
2 1 3 5
# 分组的最后一位减 第一位
f =lambda x: x.test3.iloc[-1]-x.test3.iloc[0]
frame.groupby([frame.id01]).apply(f)
id01
1 2
dtype: