简体   繁体   中英

How to isolate the maximum value in a data frame in pandas python library

I'm trying to isolate and print the maximum value in a pandas dataframe in python.

# Data frame:

df
>>     0   A   B   C
    0  0   0   0   0
    A  0  -3  -3   5
    B  0  -3  -6   2
    C  0   5   0  -3
    D  0   5   2  -3
    E  0   0  10   5
    F  0  -3   5  15

I have managed to isolate the value with the following code:

x = df.max(axis=0)
maxValue = max(x)

maxValue
>> 15

But how can I access this element? Is there a way to iterate through the elements of the data frame such that

for elements in df:
    if element == maxValue:
        m = element

Or something on those lines? I need to find the largest element, in this case 15, and retrieve its position ie (C,F) in this example. I then need to store this and then find the next largest element surrounding the first, along with its position.

# desired output
[(C,F), (B,E), (A,D)]

I hope this makes sense! Any advice on how I could implement this would be much appreciated! :)

I understand question is necessary sorting maximal values, so use if nedd omit first column DataFrame.iloc , then DataFrame.agg for positions of maximums with max for maximums, sorting them by DataFrame.sort_values , select it to Series and last convert to list of tuples:

L = (list(df.iloc[:, 1:]
            .agg(['idxmax','max'])
            .sort_values('max', axis=1, ascending=False)
            .loc['idxmax'].items()))
print (L)
[('C', 'F'), ('B', 'E'), ('A', 'C')]

For all columns remove iloc :

L = (list(df.agg(['idxmax','max'])
            .sort_values('max', axis=1, ascending=False)
            .loc['idxmax'].items()))
print (L)
[('C', 'F'), ('B', 'E'), ('A', 'C'), ('0', '0')]

You can use:

#replace 'df.iloc[:,1:]' with 'df' if first column isnt 0
out = [*df.iloc[:,1:][::-1].idxmax().items()] 
#[('A', 'D'), ('B', 'E'), ('C', 'F')]

IIUC sort_values + stack

df.stack().sort_values().groupby(level=1).tail(1).index.tolist()
Out[229]: [('A', '0'), ('D', 'A'), ('E', 'B'), ('F', 'C')]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM