Pythonic
Pythonic

Reputation: 2131

Pandas - Variation on filling NaN - What's an elegant and pythonic way to do this?

Good evening

In the dataframe below, column 'c' has a few NaNs

What is a nice, pythonic way to fill the first N nans with a value and the remaining nans with another one

(example: fill the first 3 nans with the value 10 and the remaining 2 nans with the value 20)

Thanks

   a  b   c
a  5  5 NaN
b  5  8   8
c  0  1 NaN
d  8  5   6
e  1  6 NaN
f  2  5   8
g  6  5   5
h  0  1   3
i  7  3 NaN
j  2  6 NaN

Edit I - This is one (un-pythonic) way:

nan_number = df['c'].isnull().cumsum()[df['c'].isnull()] 
df['c'][nan_number.index[nan_number<=3]] = 10
df['c'][nan_number.index[nan_number>3]] = 20

Edit II - This starts to look better:

nan_rows = df.index[df['c'].isnull()]
df.loc[nan_rows[:3], 'c'] = 10  
df.loc[nan_rows[3:], 'c'] = 20

Upvotes: 1

Views: 88

Answers (1)

EdChum
EdChum

Reputation: 394159

You could use fillna, this takes a limit param:

In [75]:

df = df.fillna(10,limit=3)
df = df.fillna(20)
df
Out[75]:
   a  b   c
a  5  5  10
b  5  8   8
c  0  1  10
d  8  5   6
e  1  6  10
f  2  5   8
g  6  5   5
h  0  1   3
i  7  3  20
j  2  6  20

If you prefer a one-liner you can chain the calls to fillna:

In [80]:

df = df.fillna(10,limit=3).fillna(20)
df
Out[80]:
   a  b   c
a  5  5  10
b  5  8   8
c  0  1  10
d  8  5   6
e  1  6  10
f  2  5   8
g  6  5   5
h  0  1   3
i  7  3  20
j  2  6  20

Upvotes: 4

Related Questions