问题
I have a df with columns X1, Y1, Z3.
df.describe
shows the stats for each column
I would like to extract the min, max and std for say column Z3.
df[df.z3].idxmax()
doesn't seem to work
回答1:
Demo:
In [7]: df = pd.DataFrame(np.random.rand(10, 3), columns=['X1','Y1','Z3'])
In [8]: df
Out[8]:
X1 Y1 Z3
0 0.258116 0.667943 0.954830
1 0.584975 0.546284 0.045745
2 0.698974 0.409223 0.307409
3 0.073166 0.356393 0.722233
4 0.339093 0.146043 0.614686
5 0.624361 0.062805 0.574546
6 0.886631 0.217291 0.258432
7 0.403345 0.035377 0.096881
8 0.663185 0.376171 0.593964
9 0.789347 0.489057 0.564688
In [9]: df.describe()
Out[9]:
X1 Y1 Z3
count 10.000000 10.000000 10.000000
mean 0.532119 0.330659 0.473341
std 0.255544 0.210851 0.287745
min 0.073166 0.035377 0.045745
25% 0.355156 0.163855 0.270676
50% 0.604668 0.366282 0.569617
75% 0.690027 0.469099 0.609506
max 0.886631 0.667943 0.954830
In [10]: df.describe().loc[['min','max','std'], 'Z3']
Out[10]:
min 0.045745
max 0.954830
std 0.287745
Name: Z3, dtype: float64
回答2:
New in pandas 0.20.0
, pd.DataFrame.agg
df.agg(['min', 'max', 'std'])
X1 Y1 Z3
min 0.074689 0.032109 0.074912
max 0.833935 0.981217 0.840831
std 0.252231 0.315192 0.294872
来源:https://stackoverflow.com/questions/45740018/extracting-the-max-min-or-std-from-a-df-for-a-particular-column-in-pandas