1

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.

Catherine
  • 161
  • 6

2 Answers2

0

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
>>> 
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
0

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()
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Hello, I accepted your answer. But then I found out another column in my dataframe that I want to the same process actually have duplicates. That column is: ``` "", "c", "","c","", "C", "","d"``` The bfill() does not work very well there. Do you mind give some inputs for that? Thanks! – Catherine Sep 24 '21 at 09:03
  • @Catherine - Another solution not working? – jezrael Sep 24 '21 at 09:04
  • You mean ```df.replace('', np.nan).bfill()```. I want specify a specific column and backfill. Is that possible? Thanks! – Catherine Sep 24 '21 at 15:34
  • @Catherine answer was edited – jezrael Sep 24 '21 at 17:33