【问题标题】:How to find month gaps count from monthly data?如何从月度数据中找出月差数?
【发布时间】:2018-06-26 18:51:34
【问题描述】:

我有一个如下数据框:

name,date
AAA,201705
AAA,201706
AAA,201707
AAA,201708
AAA,201710
AAA,201711
AAA,201802
AAA,201803
AAA,201804
AAA,201805
AAA,201806
AAA,201807

在此数据框中,有两列可用,即名称和日期。在日期列中,只有 yyyymm 格式的年份和月份可用。

在日期列中,201709、201712 和 201801 月份的值不可用。

需要检查所有月份是否存在。如果任何月份不可用,则需要以下格式的输出:

name,start_date,end_date,count
AAA,201709,201709,1
AAA,201712,201801,2

我正在尝试使用pandas diff function

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    使用asfreq:

    #convert column to datetimes
    df['date'] = pd.to_datetime(df['date'], format='%Y%m')
    # get missing values by asfreq
    a = df.set_index('date').groupby('name')['name'].apply(lambda x: x.asfreq('MS'))
    #filter only NaNs consecutive rows
    b = a.notnull().cumsum()[a.isnull()].reset_index(name='g')
    
    #aggregate first, last and count
    d = {'date':['first','last'],'name':['first', 'size']}
    df = b.groupby('g').agg(d).reset_index(drop=True)
    #data cleaning
    df.columns = df.columns.map('_'.join)
    df = df.rename(columns={'date_first':'start_date', 
                            'date_last':'end_date', 
                            'name_first':'name', 
                            'name_size':'count'})
    print (df)
      start_date   end_date name  count
    0 2017-09-01 2017-09-01  AAA      1
    1 2017-12-01 2018-01-01  AAA      2
    

    详情

    print (a)
    name  date      
    AAA   2017-05-01    AAA
          2017-06-01    AAA
          2017-07-01    AAA
          2017-08-01    AAA
          2017-09-01    NaN
          2017-10-01    AAA
          2017-11-01    AAA
          2017-12-01    NaN
          2018-01-01    NaN
          2018-02-01    AAA
          2018-03-01    AAA
          2018-04-01    AAA
          2018-05-01    AAA
          2018-06-01    AAA
          2018-07-01    AAA
    Name: name, dtype: object
    

    【讨论】:

      猜你喜欢
      • 2019-08-12
      • 2023-01-26
      • 1970-01-01
      • 2020-11-16
      • 1970-01-01
      • 2021-05-09
      • 2019-11-06
      • 2020-01-07
      • 2019-02-02
      相关资源
      最近更新 更多