Is there a python function to fill missing data with consecutive value

Viewed 553

I want to Fill in these missing numbers in column b with the consecutive values 1 and 2.

This is what I have done:

import pandas as pd
import numpy as np

df = pd.DataFrame({'a': [1, 4, 7,8,4],
'b': [1, np.nan, 3, np.nan, 5]})

df['b'].fillna({'b':[1,2]}, inplace=True)

but nothing is done.

4 Answers

You can't feed fillna a list of values, as stated here and in the documentation. Also, if you're selecting the column, no need to tell fillna which column to use. You could do:

df.fillna({'b':1}, inplace=True)

Or

df['b'].fillna(1, inplace=True)

By the way, inplace is on the way to deprecation in Pandas, the preferred way to do this is, for example

df = df.fillna({'b':1}) 

One way is to use loc with an array:

df.loc[df['b'].isnull(), 'b'] = [1, 2]

What you're attempting is possible but cumbersome with fillna:

nulls = df['b'].isnull()
df['b'] = df['b'].fillna(pd.Series([1, 2], index=nulls[nulls].index))

You may be looking for interpolate but the above solutions are generic given an input list or array.

If, on the other hand, you want to fill nulls with a sequence 1, 2, 3, etc, you can use cumsum:

# fillna solution
df['b'] = df['b'].fillna(df['b'].isnull().cumsum())

# loc solution
nulls = df['b'].isnull()
df.loc[nulls, 'b'] = nulls.cumsum()

You can interpolate. Example:

s = pd.Series([0, 1, np.nan, 3])
s.interpolate()

0    0
1    1
2    2
3    3

If I understand wording " consecutive values 1 and 2" correctly, the solution may be:

from itertools import isclice, cycle

filler = [1, 2]
nans = df.b.isna()
df.loc[nans, 'b'] = list(islice(cycle(filler), sum(nans)))
Related