简体   繁体   English

返回日期的列的最大值

[英]returns max value of a columns for a date

How can I find max value of sum column for the date 2018 Jan and returns columns A, a, b, c with a one liner code. 我如何找到日期2018 Jan的sum列的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 using drop_duplicates after sort_values , and you need a new key here for convert date format to '%y-%m' IIUC使用drop_duplicatessort_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

If need get max from all values 如果需要从所有值中获取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     

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM