【问题标题】:What does n.d. mean when using panda series python 3?nd 是什么意思?使用熊猫系列python 3时的意思?
【发布时间】:2017-09-22 17:34:02
【问题描述】:

我正在寻找 panda.core.series.Series 类的 max 值,当我使用以下代码时它返回 n.d.

rowMax = df.max(axis = 1)

问题: 什么是 n.d.意思是,我怎样才能得到一个实际值? (我的系列长度是20031)

【问题讨论】:

    标签: python-3.x pandas series


    【解决方案1】:

    我试着模拟你的问题:

    df = pd.DataFrame({'A':['1','3','4'],
                       'B':['5','6','3'],
                       'E':['3','4', 3]})
    
    print (df)
       A  B  E
    0  1  5  3
    1  3  6  4
    2  4  3  3
    
    a = df.max(axis=1)
    print (a)
    0   NaN
    1   NaN
    2   NaN
    dtype: float64
    

    这意味着您的数据是混合的 - 数字与字符串。

    解决方案是将所有数据转换为数字:

    a = df.astype(int).max(axis=1)
    print (a)
    0    5
    1    6
    2    4
    dtype: int32
    

    有时这是不可能的,因为非数字数据:

    df = pd.DataFrame({'A':['rr','3','4'],
                       'B':['5','6','3'],
                       'E':['3','4', 3]})
    
    print (df)
        A  B  E
    0  rr  5  3
    1   3  6  4
    2   4  3  3
    
    a = df.astype(int).max(axis=1)
    

    ValueError: int() 以 10 为底的无效文字:'rr'

    那么可以使用to_numeric:

    a = df.apply(lambda x: pd.to_numeric(x, errors='coerce'))
    print (a)
         A  B  E
    0  NaN  5  3
    1  3.0  6  4
    2  4.0  3  3
    
    a = df.apply(lambda x: pd.to_numeric(x, errors='coerce')).max(axis=1)
    print (a)
    0    5.0
    1    6.0
    2    4.0
    dtype: float64
    

    【讨论】:

      【解决方案2】:

      如果它确实是一个系列而不是一个数据框,max 方法应该可以在没有任何参数的情况下工作。

      s = pd.Series({'a' : 0., 'b' : 1., 'c' : 2.})
      s.max()
      
      > 2
      

      你确定你不是在处理数据框吗?

      【讨论】:

      • 这是一个系列,但我仔细查看了它最初来自的数据框,有很多行带有 n.d. 值。我想更大的问题是处理这些行。关于如何跳过或忽略这些行的任何建议,或者我应该在 SO 上发布一个新问题吗?
      • 系列的 dtype 是什么,是“n.d.”字符串?
      猜你喜欢
      • 2020-01-31
      • 2014-04-04
      • 2021-01-09
      • 1970-01-01
      • 1970-01-01
      • 2010-09-29
      相关资源
      最近更新 更多