I have a very big DataFrame that looks like:
c1 c2 c3
0 NaN 1.0 NaN
1 NaN NaN NaN
2 3.0 6.0 9.0
3 NaN 7.0 10.0
...
I want to:
1- Delete the rows with all "Nan" values. like the second row 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]
Update: When we don't want to consider the mean of all rows. sample dataframe:
import pandas as pd
import numpy as np
df = pd.DataFrame()
df['id'] = [1, 2, 3, 4, 5]
df['c1'] = [np.nan, np.nan, 3, np.nan, 5]
df['c2'] = [1, np.nan, 3, 11, 5]
df['c3'] = [1, np.nan, 3, 11, np.nan]
df['c4'] = [3, np.nan, 3, 11, 5]
output:
df = pd.DataFrame()
df['id'] = [1, 3, 4, 5]
df['c1'] = [ 5/3, 3, 11, 5]
df['c2'] = [1, 3, 11, 5]
df['c3'] = [1, 3, 11, 5]
df['c4'] = [3, 3, 11, 5]
df
For this part, I don't want to consider the value of id for calculating the mean of row.