vendredi 30 juillet 2021

Output of for combined loop and nested if is not coming out as intended for pandas

From a Pandas dataframe, I am trying to get an output of dates and its corresponding holiday type (eg: 25-Dec-2021 & Christmas) printed. However, the output only works as intended for one of them.

The dataframe holiday_sales_4 is as follows:

    Date        Weekly_Sales
0   2010-02-12  True
1   2010-09-10  False
2   2010-11-26  True
3   2010-12-31  False
4   2011-02-11  True
5   2011-09-09  False
6   2011-11-25  True
7   2011-12-30  False
8   2012-02-10  True
9   2012-09-07  True

and i'm trying with this code:

for i in range(0, len(holiday_sales_4)):
    comparison=holiday_sales_4.iloc[i]['Weekly_Sales']
    if comparison == True:
        date_of_true=holiday_sales_4.iloc[i]['Date'].strftime('%d-%b-%y')
        if (date_of_true==datetime.datetime(2010, 2, 12) or 
            date_of_true==datetime.datetime(2011, 2, 11) or 
            date_of_true==datetime.datetime(2012, 2, 10) or 
            date_of_true==datetime.datetime(2013, 2, 18)):
            return_holiday="Superbowl"
        elif (date_of_true==datetime.datetime(2010, 9, 10) or 
              date_of_true==datetime.datetime(2011, 9, 11) or 
              date_of_true==datetime.datetime(2012, 9, 12) or 
              date_of_true==datetime.datetime(2013, 9, 13)):
            return_holiday="Labour Day"
        elif (date_of_true==datetime.datetime(2010, 11, 26) or 
              date_of_true==datetime.datetime(2011, 11, 25) or 
              date_of_true==datetime.datetime(2012, 11, 23) or 
              date_of_true==datetime.datetime(2013, 11, 29)):
            return_holiday="Thanksgiving"
        elif (date_of_true==datetime.datetime(2010, 12, 31) or 
              date_of_true==datetime.datetime(2011, 12, 30) or 
              date_of_true==datetime.datetime(2012, 12, 28) or 
              date_of_true==datetime.datetime(2013, 12, 27)):
            return_holiday="Christmas"
            
        print(date_of_true)
        print(return_holiday,'test')

The output seems to iterate the loop/if condition correctly for date_of_true, but not for return_holiday:

The dates of holidays where Weekly Sales are greater than the mean sales of the non-holiday season are: 

12-Feb-10
07-Sep-12 test
26-Nov-10
07-Sep-12 test
11-Feb-11
07-Sep-12 test
25-Nov-11
07-Sep-12 test
10-Feb-12
07-Sep-12 test
07-Sep-12
07-Sep-12 test

Im new to coding, so iim sure the solution is simple, but I can't find it!

Aucun commentaire:

Enregistrer un commentaire