How to get max value from data frame using pandas?
df = pd.DataFrame({'one' : [0,1,3,2,0,1],'two' : [0,5,1,0,1,1]})
I am using:
df1 = df.max()
This gives correct output but the data frame format is changed
I want to retain the original format of dataframe as below:
df1 = pd.DataFrame({'one' : [3],'two' : [5]})
To find the maximum value of each column, call the max() method on the Dataframe object without taking any argument. In the output, We can see that it returned a series of maximum values where the index is the column name and values are the maxima from each column.
Find Maximum Element in Pandas DataFrame's RowIf the axis equals to 0, the max() method will find the max element of each column. On the other hand, if the axis equals to 1, the max() will find the max element of each row.
In the pandas series constructor, there is a method called argmax() which is used to get the position of maximum value over the series data. The pandas series is a single-dimensional data structure object with row index values. By using row index values we can access the data.
In Python, there is a built-in function max() you can use to find the largest number in a list. To use it, call the max() on a list of numbers. It then returns the greatest number in that list.
option 1pandas
with to_frame
and transpose
df.max().to_frame().T
# pd.DataFrame(df.max()).T
one two
0 3 5
option 2numpy
with [None, :]
and pd.DataFrame
constructor
pd.DataFrame(df.values.max(0)[None, :], columns=df.columns)
one two
0 3 5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With