drop values based on type pandas 1

drop values based on type pandas

df[df.columns[n]] = df[df.columns[n]].apply(pd.to_numeric, errors='coerce').fillna(0).astype(float).dropna()

Here is what the above code is Doing:
1. We’re using the apply function to apply a function to each column in the dataframe.
2. The function we’re applying is pd.to_numeric, which converts the values in the column to numeric values.
3. We’re using the errors=’coerce’ argument to tell pandas to set any values that can’t be converted to numeric values to NaN.
4. We’re using the fillna function to replace any NaN values with 0.
5. We’re using the astype function to convert the column to a float.
6. We’re using the dropna function to drop any rows that have NaN values.

Similar Posts