Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find Max from each column in data frame

Tags:

python

pandas

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]})
like image 786
onkar Avatar asked Apr 20 '17 19:04

onkar


People also ask

How do you find the maximum number in a data frame?

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.

How do you get max rows in pandas?

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.

Where is Max value in Panda series?

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.

How do you return a max value from a list in Python?

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.


1 Answers

option 1
pandas with to_frame and transpose

df.max().to_frame().T
# pd.DataFrame(df.max()).T

   one  two
0    3    5

option 2
numpy with [None, :] and pd.DataFrame constructor

pd.DataFrame(df.values.max(0)[None, :], columns=df.columns)

   one  two
0    3    5
like image 181
piRSquared Avatar answered Sep 30 '22 15:09

piRSquared