【问题标题】:Pandas bfill and ffill how to use for numeric and non-numeric columnsPandas bfill 和 ffill 如何用于数字和非数字列
【发布时间】:2020-05-17 23:37:00
【问题描述】:

我的一些 NAN 是字符串,而我的一些 NAN 是数字缺失值,如何在这两种情况下使用 bfill 和 ffill?

df

Criteria      Col1   Col2   Col3     Col4
Jan10Sales     12      13     NAN     NAN
Feb10Sales     1        3      4      ABC
Mar10Sales      NAN      13     14    XY
Apr10Sales      5      NAN     12      V 
May10Sales      6      18     19      AB

【问题讨论】:

    标签: python pandas


    【解决方案1】:
    I guess string 'NAN' does not mean Non-Value Nan, you already got the solution, you can check my code too
    
    df = df[df.ne('NAN')].bfill()
    
         Criteria Col1 Col2 Col3
    0  Jan10Sales   12   13    4
    1  Feb10Sales    1    3    4
    2  Mar10Sales    5   13   14
    3  Apr10Sales    5   18   12
    4  May10Sales    6   18   19
    

    【讨论】:

      【解决方案2】:

      如果NaNs 缺少值,您可以传递诸如list 之类的列名称:

      cols = ['Col1','Col2','Col3']
      df[cols]=df[cols].bfill()
      

      如果NaNs 是字符串,首先将字符串替换为带有非数字缺失值的数字:

      cols = ['Col1','Col2','Col3']
      df[cols]=df[cols].apply(lambda x: pd.to_numeric(x, errors='coerce')).bfill()
      

      如果想使用您的解决方案:

      for col in ['Col1','Col2','Col3']:
          df[col]= pd.to_numeric(df[col], errors='coerce').bfill()
      
      print (df)
           Criteria  Col1  Col2  Col3
      0  Jan10Sales  12.0  13.0   4.0
      1  Feb10Sales   1.0   3.0   4.0
      2  Mar10Sales   5.0  13.0  14.0
      3  Apr10Sales   5.0  18.0  12.0
      4  May10Sales   6.0  18.0  19.0
      

      但如果最后一行有缺失值,则回填不会替换它们,因为不存在下一个非缺失值:

      print (df)
           Criteria Col1 Col2 Col3
      0  Jan10Sales   12   13  NAN
      1  Feb10Sales    1    3    4
      2  Mar10Sales  NAN   13   14
      3  Apr10Sales    5  NAN   12
      4  May10Sales    6   18  NaN
      
      cols = ['Col1','Col2','Col3']
      df[cols]=df[cols].apply(lambda x: pd.to_numeric(x, errors='coerce')).bfill()
      print (df)
      
           Criteria  Col1  Col2  Col3
      0  Jan10Sales  12.0  13.0   4.0
      1  Feb10Sales   1.0   3.0   4.0
      2  Mar10Sales   5.0  13.0  14.0
      3  Apr10Sales   5.0  18.0  12.0
      4  May10Sales   6.0  18.0   NaN
      

      那么是可能的链bfillffill

      df[cols]=df[cols].apply(lambda x: pd.to_numeric(x, errors='coerce')).bfill().ffill()
      print (df)
           Criteria  Col1  Col2  Col3
      0  Jan10Sales  12.0  13.0   4.0
      1  Feb10Sales   1.0   3.0   4.0
      2  Mar10Sales   5.0  13.0  14.0
      3  Apr10Sales   5.0  18.0  12.0
      4  May10Sales   6.0  18.0  12.0
      

      【讨论】:

      • 我可以在for循环中使用这个吗
      • @ShailajaGuptaKapoor - 是的,有可能,答案已被编辑。
      • 仍然在做df.columns[df.isna().any()].tolist() 我正在获取之前应用 bfill 的所有列
      • @ShailajaGuptaKapoor - print (df[cols].dtypes) 是什么?
      • @ShailajaGuptaKapoor - 数据是保密的?因为如果使用样本数据非常好,那么数据应该有一些问题。
      【解决方案3】:

      你可以试试这个:

      for cols in ['Col1','Col2','Col3']:
          df[cols].fillna(method='bfill', inplace=True)
      

      pandas.DataFrame.fillna

      【讨论】:

        猜你喜欢
        • 2022-10-24
        • 1970-01-01
        • 2019-01-07
        • 2022-07-27
        • 1970-01-01
        • 2021-12-11
        相关资源
        最近更新 更多