How to replace NaNs by preceding values in pandas DataFrame?

前端 未结 9 2190
無奈伤痛
無奈伤痛 2020-11-22 06:04

Suppose I have a DataFrame with some NaNs:

>>> import pandas as pd
>>> df = pd.DataFrame([[1, 2, 3], [4, None, None], [None, N         


        
9条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 06:57

    The accepted answer is perfect. I had a related but slightly different situation where I had to fill in forward but only within groups. In case someone has the same need, know that fillna works on a DataFrameGroupBy object.

    >>> example = pd.DataFrame({'number':[0,1,2,nan,4,nan,6,7,8,9],'name':list('aaabbbcccc')})
    >>> example
      name  number
    0    a     0.0
    1    a     1.0
    2    a     2.0
    3    b     NaN
    4    b     4.0
    5    b     NaN
    6    c     6.0
    7    c     7.0
    8    c     8.0
    9    c     9.0
    >>> example.groupby('name')['number'].fillna(method='ffill') # fill in row 5 but not row 3
    0    0.0
    1    1.0
    2    2.0
    3    NaN
    4    4.0
    5    4.0
    6    6.0
    7    7.0
    8    8.0
    9    9.0
    Name: number, dtype: float64
    

提交回复
热议问题