Home > Mobile >  repeats the value in one column to fill the empty cells in that column [duplicate]
repeats the value in one column to fill the empty cells in that column [duplicate]

Time:09-24

I have a dataframe like this:

A  B
a
a  
a  b
a
a  
a  B

I want to fill the empty cells in the column "B" with the existing values in "B". so that the end result will be:

A  B
a  b
a  b
a  b
a  B
a  B
a  B

I have tried the idea to get the column "B" in a pandas series and remove the empty cells.

tmp=df['B']
tmp.dropna(axis=0, inplace=True, how=None)

Then I want to repeat each item in the tmp series three times and put it back to the origianl dataframe. But failed.

My solution may not be a good one. Any suggestion could help!

Thanks in advance.

CodePudding user response:

You need to replace the empty strings with replace, then use bfill, backward fill:

>>> df.replace('', np.nan).bfill()
   A  B
0  a  b
1  a  b
2  a  b
3  a  B
4  a  B
5  a  B
>>> 

CodePudding user response:

I cannot find duplicate, so use bfill only if empty values are missing values in some column:

df["B"] = df["B"].bfill()
  • Related