piRSquared
piRSquared

Reputation: 294228

DataFrame backfill with specific value

Setup

Consider

df = pd.DataFrame(index=pd.date_range('2016-03-01', '2016-03-08'), columns=list('ABC'))
df.iloc[2, 1] = 2.
df.iloc[5, 1] = 3.
df.iloc[7, 2] = 4.
print df

              A    B    C
2016-03-01  NaN  NaN  NaN
2016-03-02  NaN  NaN  NaN
2016-03-03  NaN    2  NaN
2016-03-04  NaN  NaN  NaN
2016-03-05  NaN  NaN  NaN
2016-03-06  NaN    3  NaN
2016-03-07  NaN  NaN  NaN
2016-03-08  NaN  NaN    4

I want to backfill NaN's only just one prior to non-NaN values with a the specific value of 1.

I could try to do df.bfill(limit=1) and I'd get:

             A    B    C
2016-03-01 NaN  NaN  NaN
2016-03-02 NaN  2.0  NaN
2016-03-03 NaN  2.0  NaN
2016-03-04 NaN  NaN  NaN
2016-03-05 NaN  3.0  NaN
2016-03-06 NaN  3.0  NaN
2016-03-07 NaN  NaN  4.0
2016-03-08 NaN  NaN  4.0

but that fills in the incorrect values.

I want the result to look like this:

              A    B    C
2016-03-01  NaN  NaN  NaN
2016-03-02  NaN    1  NaN
2016-03-03  NaN    2  NaN
2016-03-04  NaN  NaN  NaN
2016-03-05  NaN    1  NaN
2016-03-06  NaN    3  NaN
2016-03-07  NaN  NaN    1
2016-03-08  NaN  NaN    4

Upvotes: 1

Views: 370

Answers (3)

EdChum
EdChum

Reputation: 393993

A simple thing would be to mask the df on whether the df shifted values are notnull:

In [80]:
df[df.shift(-1).notnull()] = 1
df

Out[80]:
              A    B    C
2016-03-01  NaN  NaN  NaN
2016-03-02  NaN    1  NaN
2016-03-03  NaN    2  NaN
2016-03-04  NaN  NaN  NaN
2016-03-05  NaN    1  NaN
2016-03-06  NaN    3  NaN
2016-03-07  NaN  NaN    1
2016-03-08  NaN  NaN    4

Update

As pointed out by @Alexander this fails if we have consecutive non-null elements, a better method would be just to mask the whole df same as @Alexander does:

In [101]:    
df = pd.DataFrame(index=pd.date_range('2016-03-01', '2016-03-08'), columns=list('ABC'))
df.iloc[2, 1] = 2
df.iloc[5, 1] = 3
df.iloc[7, 2] = 4
df.iloc[6, 1] = 3
df

Out[101]:
              A    B    C
2016-03-01  NaN  NaN  NaN
2016-03-02  NaN  NaN  NaN
2016-03-03  NaN    2  NaN
2016-03-04  NaN  NaN  NaN
2016-03-05  NaN  NaN  NaN
2016-03-06  NaN    3  NaN
2016-03-07  NaN    3  NaN
2016-03-08  NaN  NaN    4

In [102]:
df[(df.shift(-1).notnull()) & (df.isnull())] = 1
df

Out[102]:
              A    B    C
2016-03-01  NaN  NaN  NaN
2016-03-02  NaN    1  NaN
2016-03-03  NaN    2  NaN
2016-03-04  NaN  NaN  NaN
2016-03-05  NaN    1  NaN
2016-03-06  NaN    3  NaN
2016-03-07  NaN    3    1
2016-03-08  NaN  NaN    4

Upvotes: 2

root
root

Reputation: 33793

You can use mask:

df.mask(df.isnull() & df.shift(-1).notnull(), 1, inplace=True)

Upvotes: 4

Alexander
Alexander

Reputation: 109526

for col in df:
    df[col].loc[(df[col].shift(-1).notnull()) & (df[col].isnull())] = 1

>>> df
              A    B    C
2016-03-01  NaN  NaN  NaN
2016-03-02  NaN    1  NaN
2016-03-03  NaN    2  NaN
2016-03-04  NaN  NaN  NaN
2016-03-05  NaN    1  NaN
2016-03-06  NaN    3  NaN
2016-03-07  NaN  NaN    1
2016-03-08  NaN  NaN    4

Upvotes: 1

Related Questions