python - drop rows with errors for pandas data coercion -
i have dataframe, need convert columns floats , ints, has bad rows, ie., values in column should float or integer instead string values.
if use df.bad.astype(float)
, error, expected.
if use df.bad.astype(float, errors='coerce')
, or pd.to_numeric(df.bad, errors='coerce')
, bad values replaced np.nan
, according spec , reasonable.
there errors='ignore'
, option ignores errors , leaves erroring values alone.
but actually, want not ignore errors, drop rows bad values. how can this?
i can ignore errors , type checking, that's not ideal solution, , there might more idiomatic this.
example
test = pd.dataframe(["3", "4", "problem"], columns=["bad"]) test.bad.astype(float) ## valueerror: not convert string float: 'problem'
i want this:
pd.to_numeric(df.bad, errors='drop')
and returns dataframe 2 rows.
since bad values replaced np.nan
not df.dropna()
rid of bad rows now?
edit: since need not drop initial nans, maybe use df.fillna()
prior using pd.to_numeric
Comments
Post a Comment