python - Pandas: Sorting columns by their mean value -
i have dataframe in pandas, sort columns (i.e. new dataframe, or view) according mean value of columns (or e.g. std value). documentation talks sorting label or value, not find on custom sorting methods.
how can this?
you can use mean
dataframe method , series sort_values
method:
in [11]: df = pd.dataframe(np.random.randn(4,4), columns=list('abcd')) in [12]: df out[12]: b c d 0 0.933069 1.432486 0.288637 -1.867853 1 -0.455952 -0.725268 0.339908 1.318175 2 -0.894331 0.573868 1.116137 0.508845 3 0.661572 0.819360 -0.527327 -0.925478 in [13]: df.mean() out[13]: 0.061089 b 0.525112 c 0.304339 d -0.241578 dtype: float64 in [14]: df.mean().sort_values() out[14]: d -0.241578 0.061089 c 0.304339 b 0.525112 dtype: float64
then can reorder columns using reindex_axis
:
in [15]: df.reindex_axis(df.mean().sort_values().index, axis=1) out[15]: d c b 0 -1.867853 0.933069 0.288637 1.432486 1 1.318175 -0.455952 0.339908 -0.725268 2 0.508845 -0.894331 1.116137 0.573868 3 -0.925478 0.661572 -0.527327 0.819360
note: in earlier versions of pandas, sort_values
used order
, order
deprecated part of 0.17 more consistent other sorting methods.
Comments
Post a Comment