pythonpandasdataframegoogle-trends

Google Trends - delete last row if isPartial is true


Asking this question mostly to people who use google trends, although it is quite a generic one.

My code in Google Trends returns a df such as the one below.

              1                  2                  3
              Spain   isPartial  France  isPartial  Italy  isPartial
date
2020-10-11      41       False     37     False      25      False
2020-10-18      40       False     39     False      23      False
2020-10-25      42       True      43     True       25      True      

Naturally, I have tried many ways to delete the last row, in case it contains True.


1) df2 = df[~df['isPartial].str.contains('True')]   --> AttributeError: 'DataFrame' object has no attribute 'str'

2) df2 = df[~df.eq('True').any(1)]   --> does nothing

3) df2 = df.[~[df[.iloc[:,1]].str.contains('True')] --> AttributeError: 'list' object has no attribute 'str'

What should I do?

Thank you.


Solution

  • Use DataFrame.xs for select by second level of MultiIndex in columns:

    df1 = df[~df.xs('isPartial', axis=1, level=1).any(1)]
    print (df1)
                   1                2               3          
               Spain isPartial France isPartial Italy isPartial
    date                                                       
    2020-10-11    41     False     37     False    25     False
    2020-10-18    40     False     39     False    23     False
    

    Detail:

    print (df.xs('isPartial', axis=1, level=1))
                    1      2      3
    date                           
    2020-10-11  False  False  False
    2020-10-18  False  False  False
    2020-10-25   True   True   True