william007
william007

Reputation: 18545

back fill according to other column

df=pd.DataFrame({"Date":[date(2019,10,1),date(2019,10,2),date(2019,10,1),date(2019,10,2),date(2019,10,1),date(2019,10,2),date(2019,10,1),date(2019,10,2)],
                "CatID":[1,1,1,None,2,2,2,2],
                 "ShopID":[1,1,1,1,2,2,2,2]
                })

df.CatID=df['CatID'].bfill()
df

For the None value in CatID, I want it to be back filled according to ShopID. the None value is filled by 2 which is not what I want (as value of CatID is from ShopID=2), it should remain as None, what should I do?

Upvotes: 0

Views: 30

Answers (1)

Quant Christo
Quant Christo

Reputation: 1430

I'm not sure what exactly you want to achieve:

import pandas as pd
from datetime import date


df=pd.DataFrame({"Date":[date(2019,10,1),date(2019,10,2),date(2019,10,1),date(2019,10,2),date(2019,10,1),date(2019,10,2),date(2019,10,1),date(2019,10,2)],
                "CatID":[1,1,1,None,2,2,2,2],
                 "ShopID":[1,1,1,1,2,2,2,2]
                })

df['CatID_copy'] = df['CatID']
df['CatID'] = df['CatID'].bfill()
df.loc[df['CatID_copy'].isna(), 'CatID'] = df['CatID_copy'] 
df.drop(columns='CatID_copy', inplace=True)

Output:

   Date         CatID  ShopID
0  2019-10-01    1.0       1
1  2019-10-02    1.0       1
2  2019-10-01    1.0       1
3  2019-10-02    NaN       1
4  2019-10-01    2.0       2
5  2019-10-02    2.0       2
6  2019-10-01    2.0       2
7  2019-10-02    2.0       2

If something different, please attach expected output.

Upvotes: 1

Related Questions