Find Max from each column in dataframe

How can I get the maximum value from a dataframe using pandas?

df = pd.DataFrame({'one' : [0,1,3,2,0,1],'two' : [0,5,1,0,1,1]})

      

I use:

df1=df.max()

      

This gives the correct result, but the format of the dataframe changes

I want to keep the original data format as shown below:

df1 = pd.DataFrame({'one' : [3],'two' : [5]})

      

+3


source to share


1 answer


option 1
pandas

with to_frame

andtranspose

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

   one  two
0    3    5

      



option 2
numpy

with constructor [None, :]

andpd.DataFrame

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

   one  two
0    3    5

      

+3


source







All Articles