[英]Get difference in values associated with min and max dates within group - Python
Here is the dataframe:这是数据框:
ID Date1 Date2 weight
123 1/1/2018 12/31/2018 147
123 1/1/2018 11/30/2018 136
123 1/1/2018 10/30/2018 128
123 1/1/2018 4/30/2000 150
123 5/5/2017 4/4/2017 160
123 5/5/2017 1/1/2016 170
524 4/4/2017 4/3/2017 180
524 4/4/2017 4/1/2017 150
524 4/4/2017 3/31/2017 130
524 3/3/2017 2/2/2017 210
524 3/3/2017 1/1/2017 250
524 2/3/2017 1/3/2017 230
For every ID and Date1 group, I want the difference in the weights associated with the minimum and maximum Date2.对于每个 ID 和 Date1 组,我想要与最小和最大 Date2 相关联的权重差异。 The expected output is:
预期的输出是:
ID Date1 Weight_Diff
123 1/1/2018 -3
123 5/5/2017 -10
524 4/4/2017 50
524 3/3/2017 -40
524 2/3/2017 0
I tried the following, but to no avail:我尝试了以下方法,但无济于事:
maxdate = df.groupby(['ID','Date1'])['Date2'].idxmax()
mindate = df.groupby(['ID','Date1'])['Date2'].idxmin()
df['diff'] = df.iloc[maxdate]['weight'] - df.iloc[mindate]['weight']
I think the most readable thing I could come up with was:我认为我能想到的最易读的事情是:
g = df.groupby(['ID','Date1'])
diff = g.nth(0)['weight'] - g.nth(-1)['weight']
print(diff.reset_index())
Assumption : You already sorted the columns date1 and date2假设:您已经对 date1 和 date2 列进行了排序
Returns:返回:
ID Date1 weight
0 123 1/1/2018 -3
1 123 5/5/2017 -10
2 524 2/3/2017 0
3 524 3/3/2017 -40
4 524 4/4/2017 50
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.