Python 返回日期列的最大值

Python 返回日期列的最大值,python,pandas,Python,Pandas,如何查找日期2018年1月的总和列的max值,并返回带有一行代码的列A、A、b、c Date A sum a b c d 0 2018-01-19 user1 1.82 -0.22 -0.41 0.02 0.65 1 2018-01-20 user2 1.75 -0.29 -0.42 -0.26 0.53

如何查找日期
2018年1月
的总和列的
max
值,并返回带有一行代码的列
A、A、b、c

           Date       A         sum       a       b      c       d
   0    2018-01-19  user1       1.82    -0.22   -0.41    0.02   0.65
   1    2018-01-20  user2       1.75    -0.29   -0.42   -0.26   0.53
   2    2018-01-21  user3       1.55    -0.30   -0.51   -0.28   0.45    

IIUC使用
drop\u duplicates
sort\u values
之后进行排序,您需要在此处使用一个新键将日期格式转换为“%y-%m”

df['Year-month']=pd.to_datetime(df.Date).dt.strftime('%y-%m')
df.sort_values('sum').drop_duplicates('Year-month',keep='last')
Out[1064]: 
         Date      A   sum     a     b     c     d Year-month
0  2018-01-19  user1  1.82 -0.22 -0.41  0.02  0.65      18-01
如果需要,从所有值中获取
max

df.groupby(pd.to_datetime(df.Date).dt.strftime('%Y-%B')).max()
Out[1065]: 
                    Date      A   sum     a     b     c     d 
Date                                                                    
2018-January  2018-01-21  user3  1.82 -0.22 -0.41  0.02  0.65     

我如何才能只获取
sum
列和日期
2018-01-19
的最大值,并使用pandas
loc
返回结果中的d列?@user456
df.sort_值('sum')。删除重复项('Year-month',keep='last')['d']