Python-在pandas数据框中获取最新日期

时间:2018-11-04 18:28:03

标签: python pandas dataframe pandas-groupby

我必须获取数据框熊猫中的最新日期。 我写了这段代码:

  df=pd.read_csv('waterquality.csv')
  # Convert date to datetime type
  df['SampleDate'] = df['SampleDate'].astype('datetime64[ns]')
  #print(df.SampleDate.max())

  g=df.groupby(['name','beach','ParameterCode']).SampleDate.max()
  print(g)

输出为:

  [...]
  Seal Beach        Seal Beach Pier     Enterococcus      2018-12-09
                                        Total Coliforms   2018-12-09

但是日期不是最近的。谁能帮我?谢谢

1 个答案:

答案 0 :(得分:0)

我测试了您的代码,它似乎可以正常工作,能否提供一个示例,而不是这样:

df = pd.DataFrame({'name': ['Seal Beach']*4, 'beach':['Seal Beach Pier']*4, 'ParameterCode':['Enterococcus']*2 + ['Total Coliforms']*2, 'SampleDate':['2018-12-09','2018-12-10','2018-12-09','2018-12-10']})

df

    name        beach      ParameterCode        SampleDate
0   Seal Beach  Seal Beach Pier Enterococcus    2018-12-09
1   Seal Beach  Seal Beach Pier Enterococcus    2018-12-10
2   Seal Beach  Seal Beach Pier Total Coliforms 2018-12-09
3   Seal Beach  Seal Beach Pier Total Coliforms 2018-12-10

df['SampleDate'] = df['SampleDate'].astype('datetime64[ns]')
df.groupby(['name', 'beach', 'ParameterCode']).SampleDate.max()

    name        beach            ParameterCode  
Seal Beach  Seal Beach Pier  Enterococcus      2018-12-10
                             Total Coliforms   2018-12-10