How to get the max/min value in Pandas DataFrame when nan value in it

前端 未结 6 895
萌比男神i
萌比男神i 2021-01-01 19:07

Since one column of my pandas dataframe has nan value, so when I want to get the max value of that column, it just return error.

>>> d         


        
相关标签:
6条回答
  • 2021-01-01 19:46

    You can use NumPy's help with np.nanmax, np.nanmin :

    In [28]: df
    Out[28]: 
       A   B  C
    0  7 NaN  8
    1  3   3  5
    2  8   1  7
    3  3   0  3
    4  8   2  7
    
    In [29]: np.nanmax(df.iloc[:, 1].values)
    Out[29]: 3.0
    
    In [30]: np.nanmin(df.iloc[:, 1].values)
    Out[30]: 0.0
    
    0 讨论(0)
  • 2021-01-01 19:52

    You can use Series.dropna.

    res = df.iloc[:, 1].dropna().max()
    
    0 讨论(0)
  • 2021-01-01 19:54

    When the df contains NaN values it reports NaN values, Using np.nanmax(df.values) gave the desired answer.

    0 讨论(0)
  • 2021-01-01 19:58

    Dataframe aggregate function.agg() will automatically ignore NaN value. df.agg({'income':'max'})

    Besides, it can also be use together with .groupby

    df.groupby('column').agg({'income':['max','mean']})

    0 讨论(0)
  • 2021-01-01 20:05

    You can set numeric_only = True when calling max:

    df.iloc[:, 1].max(numeric_only = True)
    
    0 讨论(0)
  • 2021-01-01 20:09

    if you dont use iloc or loc, it is simple as:

    df['column'].max()
    

    or

    df['column'][df.index.min():df.index.max()]
    

    or any kind of range in this second square brackets

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