python - pandas dataframe drop columns by number of nan -
i have dataframe columns containing nan. i'd drop columns number of nan. example, in following code, i'd drop column 2 or more nan. in case, column 'c' dropped , 'a' , 'b' kept. how can implement it?
import pandas pd import numpy np dff = pd.dataframe(np.random.randn(10,3), columns=list('abc')) dff.iloc[3,0] = np.nan dff.iloc[6,1] = np.nan dff.iloc[5:8,2] = np.nan print dff
there thresh
param dropna
, need pass length of df - number of nan
values want threshold:
in [13]: dff.dropna(thresh=len(dff) - 2, axis=1) out[13]: b 0 0.517199 -0.806304 1 -0.643074 0.229602 2 0.656728 0.535155 3 nan -0.162345 4 -0.309663 -0.783539 5 1.244725 -0.274514 6 -0.254232 nan 7 -1.242430 0.228660 8 -0.311874 -0.448886 9 -0.984453 -0.755416
so above drop column not meet criteria of length of df (number of rows) - 2 number of non-na values.
Comments
Post a Comment