I have a very big Dataframe. I want to:
1- Delete the rows with all "Nan" values. like row 2 in the sample.
2- Replace all the "Nan" values in other rows with the mean of the rows.
Note: in the rows, we have different "Nan" values.
could you please help me with that? Thanks.
Also, this link does not solve my question:
Pandas Dataframe: Replacing NaN with row average
Here is a sample of my dataframe:
import pandas as pd
import numpy as np
df = pd.DataFrame()
df['c1'] = [np.nan, np.nan, 3, np.nan]
df['c2'] = [1, np.nan, 6, 7]
df['c3'] = [np.nan, np.nan, 9, 10]
df
>Solution :
You could create a dictionary from the column names and row means and pass it to fillna to fill the NaN values. Then drop the NaN rows (which won’t get filled in because all NaN rows have mean NaN).
out = df.fillna(dict.fromkeys(df.columns, df.mean(axis=1))).dropna()
Output:
c1 c2 c3
0 1.0 1.0 1.0
2 3.0 6.0 9.0
3 8.5 7.0 10.0