谷歌趋势-如果isPartial为true,则删除最后一行



问这个问题主要是针对那些使用谷歌趋势的人,尽管这是一个很普通的问题。

我在谷歌趋势中的代码返回了一个df,比如下面的一个。

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      

当然,我已经尝试了很多方法来删除最后一行,以防它包含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'

我该怎么办?

谢谢。

使用DataFrame.xs通过第二级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

详细信息

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

最新更新