简体   繁体   中英

Multiindex with values in DataFrame in Python Pandas?

I have DataFrame like below:

df = pd.DataFrame({"ID" : ["1", "1", "1", "2", "3"],
                   "value" : [10, 11, 12, 13, 14],
                   "a" : [1,2,3,4,5]})
df.set_index(["ID", "value"], inplace=True)

在此处输入图像描述

And what can I do so as to have multiindex "ID" and "value" like below:

在此处输入图像描述

Try resetting index

df.reset_index(col_fill=['values', 'ID'])

it will create one more column which you can drop or pop from DataFrame.

The Final frame will be

ID  value  a
 1     10  1
 1     11  2
 1     12  3
 2     13  4
 3     14  5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM