How to use ffill() for n number of columns in column header

前端 未结 1 1997
离开以前
离开以前 2020-12-22 04:36

Need to fill NaN values from previous row value for n number of columns

I am trying to achieve the below through Pandas Dataframe.

  1. Creating lists of

相关标签:
1条回答
  • 2020-12-22 05:19

    Try to use df[x].fillna(y) or df[x].fillna(y, inplace=True). Here x is any column of your choice and y is any value of your choice. For example:

    1 0.0001
    2 NaN
    3 0.03
    

    gives

    1 0.0001
    2 0.0000
    3 0.03
    

    or

    list_nan_values = [1,2,3, 21, 44]   # variables list to fill NaN with
    nan_locations   = [1,2,8,19,67]     # positional location of NaN in specific column you have selected.
    
    for i in range(len(nan_locations)+1):
        df[i] = df[nan_locations[i]].fillna(list_nan_values[i])
    

    Enjoy.

    0 讨论(0)
提交回复
热议问题