Pandas(Python) : Fill empty cells with with previous row value?

后端 未结 2 1117
日久生厌
日久生厌 2020-12-28 21:36

I want to fill empty cells with with previous row value if they start with number. For example, I have

    Text    Text    
    30      Text    Text    
            


        
2条回答
  •  情话喂你
    2020-12-28 21:47

    First, replace your empty cells with NaNs:

    df[df[0]==""] = np.NaN
    

    Now, Use ffill():

    df.fillna(method='ffill')
    #       0
    #0  Text
    #1    30
    #2    30
    #3    30
    #4    31
    #5  Text
    #6    31
    #7    31
    #8    31
    #9    32
    

提交回复
热议问题