Search code examples
pythonpandasdataframeseries

repeats the value in one column to fill the empty cells in that column


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.


Solution

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

    df["B"] = df["B"].replace('', np.nan).bfill()