I have the bellow df and I'm trying to replicate the single value present in the column(Per_Extract) in the rest of the column df
IN Per_Extract
0 IN7395593 NaN
1 IN7444113 FROM : 2022-08-01 TO : 2022-10-20
2 IN7444613 FROM : 2022-08-01 TO : 2022-10-20
3 IN7444116 FROM : 2022-08-01 TO : 2022-10-20
4 IN7334213 NaN
my desired output should be this:
df
IN Per_Extract
0 IN7395593 FROM : 2022-08-01 TO : 2022-10-20
1 IN7444113 FROM : 2022-08-01 TO : 2022-10-20
2 IN7444613 FROM : 2022-08-01 TO : 2022-10-20
3 IN7444116 FROM : 2022-08-01 TO : 2022-10-20
4 IN7334213 FROM : 2022-08-01 TO : 2022-10-20
I tried ffill, but it didnt work
df['Per_Extract'] = df['Per_Extract'].ffill()
coud you guys help me?
CodePudding user response:
#replace NaN text with np.nan
# ffill and bfill null values
# Strip Per_extract with whitespaces, if present, they fail replace
df['Per_Extract']=df['Per_Extract'].str.strip().replace('NaN', np.nan).ffill().bfill()
df
IN Per_Extract
0 0 IN7395593 FROM : 2022-08-01 TO : 2022-10-20
1 1 IN7444113 FROM : 2022-08-01 TO : 2022-10-20
2 2 IN7444613 FROM : 2022-08-01 TO : 2022-10-20
3 3 IN7444116 FROM : 2022-08-01 TO : 2022-10-20
4 4 IN7334213 FROM : 2022-08-01 TO : 2022-10-20
CodePudding user response:
Or you can try:
df['Per_Extract'] = df['Per_Extract'].fillna(method='bfill').fillna(method='ffill')
df
IN Per_Extract
0 IN7395593 FROM : 2022-08-01 TO : 2022-10-20
1 IN7444113 FROM : 2022-08-01 TO : 2022-10-20
2 IN7444613 FROM : 2022-08-01 TO : 2022-10-20
3 IN7444116 FROM : 2022-08-01 TO : 2022-10-20
4 IN7334213 FROM : 2022-08-01 TO : 2022-10-20
CodePudding user response:
I would suggest the follwowing syntax
df['Per_Extract'] = df['Per_Extract'].fillna(method='ffill')