return max value from panda dataframe as a whole, not based on column or rows

后端 未结 5 1994
离开以前
离开以前 2020-11-27 21:34

I am trying to get the max value from a panda dataframe as whole. I am not interested in what row or column it came from. I am just interested in a single max value within t

相关标签:
5条回答
  • 2020-11-27 21:51

    The max of all the values in the DataFrame can be obtained using df.to_numpy().max(), or for pandas < 0.24.0 we use df.values.max():

    In [10]: df.to_numpy().max()
    Out[10]: 'f'
    

    The max is f rather than 43.0 since, in CPython2,

    In [11]: 'f' > 43.0
    Out[11]: True
    

    In CPython2, Objects of different types ... are ordered by their type names. So any str compares as greater than any int since 'str' > 'int'.

    In Python3, comparison of strings and ints raises a TypeError.


    To find the max value in the numeric columns only, use

    df.select_dtypes(include=[np.number]).max()
    
    0 讨论(0)
  • 2020-11-27 21:59

    Hi the simplest answer is the following. Answer:

    df.max().max()
    

    Explanation:
    series = df.max() give you a Series containing the maximum values for each column.
    Therefore series.max()gives you the maximum for the whole dataframe.

    :) best answers are usually the simplest

    0 讨论(0)
  • 2020-11-27 21:59

    For the max, check the previous answer... For the max of the values use e.g.:

    val_cols = [c for c in df.columns if c.startswith('val')]
    print df[val_cols].max()
    
    0 讨论(0)
  • 2020-11-27 22:05

    Max can be found in these two steps:

    maxForRow = allData.max(axis=0) #max for each row
    globalMax = maxForRow.max(); #max across all rows
    
    0 讨论(0)
  • 2020-11-27 22:10

    An alternative way:

    df.melt().value.max()
    

    Essentially melt() transforms the DataFrame into one long column.

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