Pandas version 0.23.4
, python version 3.7.1
I have a dataframe df as below
df = pd.DataFrame([[0.1, 2, 55, 0,np.nan],
[0.2, 4, np.nan, 1,99],
[0.3, np.nan, 22, 5,88],
[0.4, np.nan, np.nan, 4,77]],
columns=list('ABCDE'))
A B C D E
0 0.1 2.0 55.0 0 NaN
1 0.2 4.0 NaN 1 99.0
2 0.3 NaN 22.0 5 88.0
3 0.4 NaN NaN 4 77.0
I want to replace Na values in columns B
and C
with value in column `A'.
Expected output is
A B C D E
0 0.1 2.0 55.0 0 NaN
1 0.2 4.0 0.2 1 99.0
2 0.3 0.3 22.0 5 88.0
3 0.4 0.4 0.4 4 77.0
I have tried fillna using fill
along axis 0
, but its not giving expected output, (its filling from the above column)
df.fillna(method='ffill',axis=0, inplace = True)
A B C D E
0 0.1 2.0 55.0 0 NaN
1 0.2 4.0 55.0 1 99.0
2 0.3 4.0 22.0 5 88.0
3 0.4 4.0 22.0 4 77.0
df.fillna(method='ffill',axis=1, inplace = True)
output: NotImplementedError:
Also tried
df[['B','C']] = df[['B','C']].fillna(df.A)
output:
A B C D E
0 0.1 2.0 55.0 0 NaN
1 0.2 4.0 NaN 1 99.0
2 0.3 NaN 22.0 5 88.0
3 0.4 NaN NaN 4 77.0
Tried to fill all Na's in B
and C
with 0
, using inplace
, but this also is not giving expected output
df[['B','C']].fillna(0,inplace=True)
output:
A B C D E
0 0.1 2.0 55.0 0 NaN
1 0.2 4.0 NaN 1 99.0
2 0.3 NaN 22.0 5 88.0
3 0.4 NaN NaN 4 77.0
filling 0
to slice of data frame will work if assigned back to the same subset
df[['B','C']] = df[['B','C']].fillna(0)
output:
A B C D E
0 0.1 2.0 55.0 0 NaN
1 0.2 4.0 0.0 1 99.0
2 0.3 0.0 22.0 5 88.0
3 0.4 0.0 0.0 4 77.0
1) How to fill na values in columns B
andC
using values from column A
from the given data frame ?
2) Also why is inlace not working when using fillna on a subset of the data frame.
3) How to do ffill
along the rows(is it implemented)?