2015-10-19 211 views
1

只有1個值的所有變量我有一個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 

如何做到這一點?

回答

3

你可以applypd.Series.nunique,然後用它來選擇:在100K

>>> 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 
+0

恭喜! –