Rishab Gupta
Rishab Gupta

Reputation: 581

Python: I want to fill the null values only where the whole row is null

I have a Data frame that looks like:

Col1    Col2    Col3    Col4    Col5

A       B       C       D       E
B       X       Y       null    null
null    null    null    null    null
I       B       W       E       R

Since third row has all the null values, I want to fill one value in the third row.
The final Data frame should look like:

Col1    Col2    Col3    Col4    Col5

A       B       C       D       E
B       X       Y       null    null
xyz     null    null    null    null
I       B       W       E       R

Upvotes: 2

Views: 52

Answers (1)

jezrael
jezrael

Reputation: 862511

Use DataFrame.loc for set Col1 column by mask created by DataFrame.isna and DataFrame.all for test all True s per row:

df.loc[df.isna().all(axis=1), 'Col1'] = 'xyz'
print (df)
  Col1 Col2 Col3 Col4 Col5
0    A    B    C    D    E
1    B    X    Y  NaN  NaN
2  xyz  NaN  NaN  NaN  NaN
3    I    B    W    E    R

For fill first column:

df.iloc[df.isna().all(axis=1).values, 0] = 'xyz'

Upvotes: 6

Related Questions