获取与组内的最小日期和最大日期关联的值的差异-Python

时间:2018-10-17 04:28:12

标签: python 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

对于每个ID和Date1组,我希望与最小和最大Date2相关联的权重有所不同。预期的输出是:

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

我尝试了以下操作,但无济于事:

maxdate = df.groupby(['ID','Date1'])['Date2'].idxmax()
mindate = df.groupby(['ID','Date1'])['Date2'].idxmin()

df['diff'] = df.iloc[maxdate]['weight'] - df.iloc[mindate]['weight']

1 个答案:

答案 0 :(得分:1)

我认为我能想到的最容易理解的是:

var input = ["name", "fname", "lname", "stackOverflow"];

function transformFirstAndLast(array){
    return {[array[0]]: array.pop()}
}

responseObject = transformFirstAndLast(input)
console.log(responseObject)

假设:您已经对date1和date2列进行了排序

返回:

g = df.groupby(['ID','Date1'])
diff = g.nth(0)['weight'] - g.nth(-1)['weight']
print(diff.reset_index())