首页 > 解决方案 > pandas data frame, max of columns while keeping index

问题描述

As output I want a dataframe with only 1 row (index should be the last date) and the maximum value by column.

                A        B         C
2000-06-13 35.44110000 34.17990000 34.02230000
2000-06-14 92.11310000 91.05430000 90.95720000
2000-06-15 57.97080000 57.78140000 58.19820000
2000-06-16 34.17050000 92.45300000 58.51070000

I know I can use df.tails(n).max() but that turns the entire thing into a Series which seems rather complicated to get back into a data frame.

Does anyone know something elegant or functional to accomplish it?

标签: pythonpandastime-series

解决方案


Do you want idxmax?

df.idxmax().to_frame().T

Output

            A           B           C
0  2000-06-14  2000-06-16  2000-06-14

Or, per comments below.

df.max().to_frame().T

Output:

         A       B        C
0  92.1131  92.453  90.9572

And,

df.max().to_frame().T.rename(index={0:df.idxmax().max()})

Output:

                  A       B        C
2000-06-16  92.1131  92.453  90.9572

推荐阅读