Search code examples
pythonpandasfillna

Pandas Dataframe row value equals value above + 1


I have a dataframe with the first week of each year. I'm trying to get a list of week numbers.

So essentially I need to fill na's with the number above (1) + 1.

Here's my code so far

FirstDay= {'Date':
           { 'Wk117' :'01-07-2016'
            ,'Wk118':'30-06-2017'
            ,'Wk119':'29-06-2018'
            ,'Wk120':'28-06-2019'


   }}

FirstDay=pd.DataFrame(FirstDay).reset_index()
#
FirstDay['Date'] = pd.to_datetime(FirstDay["Date"],dayfirst=True)
FirstDay['value']=int(1)

FirstDay=FirstDay.set_index('Date')
FirstDay=FirstDay.resample('W-FRI').mean()
FirstDay=FirstDay.fillna(0)

#FirstDay['value']=FirstDay['value'].astype(int)



for index, row in FirstDay.iterrows():
     if row['value']!=float(1):
        offset=row-1
        row['value']=offset['value']+1

Does anyone see why this isn't working?

Many thanks


Solution

  • I believe you need replace missing values to 1 and then use cumsum:

    FirstDay=FirstDay.set_index('Date')
    FirstDay=FirstDay.resample('W-FRI').mean()
    FirstDay=FirstDay.fillna(1).cumsum().astype(int)
    print (FirstDay.head(10))
                value
    Date             
    2016-07-01      1
    2016-07-08      2
    2016-07-15      3
    2016-07-22      4
    2016-07-29      5
    2016-08-05      6
    2016-08-12      7
    2016-08-19      8
    2016-08-26      9
    2016-09-02     10
    

    If need reset count for each year:

    FirstDay['value'] = FirstDay.groupby(FirstDay['value'].fillna(0).cumsum()).cumcount().add(1)
    print (FirstDay.head(10))
                value
    Date             
    2016-07-01      1
    2016-07-08      2
    2016-07-15      3
    2016-07-22      4
    2016-07-29      5
    2016-08-05      6
    2016-08-12      7
    2016-08-19      8
    2016-08-26      9
    2016-09-02     10