I have DataFrame like below:
df = pd.DataFrame({"ID" : ["1", "1", "1", "2", "3"], "value" : [10, 11, 12, 13, 14]})
df.set_index(["ID", "value"], inplace=True)
By doing so I have multiindex "ID" and "value" like below:
What can I do so as to have this multiindex like on the result like below:
I tried also other way on the data frame 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 code:
#temporaly display all levels of MultiIndex
with pd.option_context('display.multi_sparse',False):
print (df)
But result is not a "elegant" DataFrame
It is about diplaying, by default are hidden repeated values, link :
display.multi_sparse, True, “Sparsify” MultiIndex display (don't display repeated elements in outer levels within groups)
df = pd.DataFrame({"ID" : ["1", "1", "1", "2", "3"], "value" : [10, 11, 12, 13, 14]})
df.set_index(["ID", "value"], inplace=True)
df['tmp'] = 1
#temporaly display all levels of MultiIndex
with pd.option_context('display.multi_sparse',False):
print (df)
tmp
ID value
1 10 1
1 11 1
1 12 1
2 13 1
3 14 1
For set always display all levels (not default option):
pd.set_option('display.multi_sparse', False)
print (df)
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.