Sunday, 15 August 2010

python - Pandas dataframe: return row AND column of maximum value(s) -



python - Pandas dataframe: return row AND column of maximum value(s) -

i have dataframe in values of same variety (e.g. correlation matrix -- expect unique maximum). i'd homecoming row , column of maximum of matrix.

i can max across rows or columns changing first argument of

df.idxmax()

however haven't found suitable way homecoming row/column index of max of whole dataframe.

for example, can in numpy:

>>>npa = np.array([[1,2,3],[4,9,5],[6,7,8]]) >>>np.where(npa == np.amax(npa)) (array([1]), array([1]))

but when seek similar in pandas:

>>>df = pd.dataframe([[1,2,3],[4,9,5],[6,7,8]],columns=list('abc'),index=list('def')) >>>df.where(df == df.max().max()) b c d nan nan nan e nan 9 nan f nan nan nan

at sec level, what acutally want homecoming rows , columns of top n values, e.g. series.

e.g. above i'd function does:

>>>topn(df,3) b e c f b f dtype: object >>>type(topn(df,3)) pandas.core.series.series

or just

>>>topn(df,3) (['b','c','b'],['e','f','f'])

a la numpy.where()

what want utilize stack

df = pd.dataframe([[1,2,3],[4,9,5],[6,7,8]],columns=list('abc'),index=list('def')) df = df.stack() df.sort(ascending=false) df.head(4) e b 9 f c 8 b 7 6 dtype: int64

python pandas

No comments:

Post a Comment