Finding max /min value of individual columns
Asked Answered
L

2

25

I am a beginner to Data Analysis using Python and stuck on the following:

I want to find maximum value from individual columns (pandas.dataframe) using Broadcasting /Vectorization methodology.

A snapshot of my data Frame is as follows:

enter image description here

Linguini answered 30/10, 2017 at 0:19 Comment(4)
I am trying to find max value using idxmax() /max() methods. e.g- df_Close['AAPL'].max()-- This gives me max value of AAPL column. However, when I try to pass a list of columns e.g df_Close[col_list].max(), it errors out saying 'TypeError: unhashable type: 'Index''Linguini
df_Close[df_Close.columns.values[col_list]].max()Ingrowing
You should be passing column names in the list, not an index objectMesotron
Got it. Thanks. :)Linguini
P
29

you can use pandas.DataFrame built-in function max and min to find it

example

df = pandas.DataFrame(randn(4,4))
df.max(axis=0) # will return max value of each column
df.max(axis=0)['AAL'] # column AAL's max
df.max(axis=1) # will return max value of each row

or another way just find that column you want and call max

df = pandas.DataFrame(randn(4,4))
df['AAL'].max()
df['AAP'].min()

min is the same

Pella answered 30/10, 2017 at 0:32 Comment(1)
I was already doing it. However, I wanted to get max values of every column. Thanks.Linguini
H
18

You can use aggregate function to get the min and max value in a single line of code.

For the entire dataset:

df.agg(['min', 'max'])

For a specific column:

df['column_name'].agg(['min', 'max'])

Hitt answered 15/8, 2022 at 20:29 Comment(1)
In addition to being a single line of code, is using agg more efficient in terms of memory access (i.e. a single pass on the data)? It matters for large dataframes.Johnnie

© 2022 - 2024 — McMap. All rights reserved.