问题描述
我有一个df:
>>>> df
Genre Rat View
Date
20111231 CNN NaN HH
20111231 CNN 210 HH
20111231 NaN 4.3 HH
我需要清理DF以获得一个新的,如下所示:
Rat
Date
20111231 NaN
20111231 210
20111231 4.3
怎么做 ?
1楼
您可以apply
pd.Series.nunique
,然后使用它来选择:
>>> df.apply(pd.Series.nunique)
Genre 1
Rat 2
View 1
dtype: int64
>>> df.loc[:, ~(df.apply(pd.Series.nunique) == 1)]
Rat
Date
20111231 NaN
20111231 210.0
20111231 4.3