大Pandas 的数据帧:
data = pd.DataFrame ({
'group': ['A', 'A', 'B', 'B', 'C', 'C'],
'date': ['2023-01-15', '2023-02-20', '2023-01-10', '2023-03-05', '2023-02-01', '2023-04-10'],
'value': [10, 15, 5, 25, 8, 12]} )
try 根据聚合函数中每个‘group’的‘Date’列的最小值和最大值来获取‘Value’列的值:
## the following doesn't work
output = (
df
.groupby(['group'],as_index=False).agg(
## there are some other additional aggregate functions happening here too.
value_at_min = ('value' , lambda x: x.loc[x['date'].idxmin()])
, value_at_max = ('value' , lambda x: x.loc[x['date'].idxmax()])
))
这不起作用,即使将Date转换为DateTime(实际上,我的原始Date列是DateTime格式).
所需输出应为:
group min_date max_date value_at_min value_at_max
0 A 2023-01-15 2023-02-20 10 15
1 B 2023-01-10 2023-03-05 5 25
2 C 2023-02-01 2023-04-10 8 12