Sort the dataframe should help
df.sort()
题
I am trying to createa multilevel index in pandas but when I do a print of hte dataframe, I see something like this.
ask bid
time type mat strk
2014-03-31 13:30:00 C 2014-04-19 1850 32.0 30.6
2014-03-31 13:35:00 C 2014-04-19 1850 32.8 31.2
2014-03-31 13:40:00 C 2014-04-19 1850 31.9 29.9
2014-03-31 13:45:00 C 2014-04-19 1850 31.6 30.1
2014-03-31 13:50:00 C 2014-04-19 1850 32.6 31.2
2014-03-31 13:55:00 C 2014-04-19 1850 33.6 32.1
2014-03-31 14:00:00 C 2014-04-19 1850 34.4 33.0
2014-03-31 14:05:00 C 2014-04-19 1850 33.5 31.9
2014-03-31 14:10:00 C 2014-04-19 1850 34.0 32.2
.....
The above is a result of
df.set_index(['time', 'type', 'mat', 'strk'], inplace=True)
Now I would have expected to see something more hierarchial like this
ask bid
time type mat strk
2014-03-31 13:30:00 C 2014-04-19 1850 32.0 30.6
1860 aaaa bbbb
2014-05-17 1850 xxxx yyyy
1860 xxxx yyyy
P 2014-04-19 1850 mmmm nnnn
1860 aaaa bbbb
2014-05-17 1850 xxxx yyyy
1860 xxxx yyyy
2014-03-31 13:35:00 C 2014-04-19 1850 32.8 31.2
2014-03-31 13:40:00 C 2014-04-19 1850 31.9 29.9
2014-03-31 13:45:00 C 2014-04-19 1850 31.6 30.1
2014-03-31 13:50:00 C 2014-04-19 1850 32.6 31.2
2014-03-31 13:55:00 C 2014-04-19 1850 33.6 32.1
2014-03-31 14:00:00 C 2014-04-19 1850 34.4 33.0
2014-03-31 14:05:00 C 2014-04-19 1850 33.5 31.9
2014-03-31 14:10:00 C 2014-04-19 1850 34.0 32.2
I have a few rows below for the same time - 2014-03-31 13:30:00 But not sure why I dont see this happeniing. Tried without that 'inplace' options. Still i see the same issue. But in a different place, I do see this hierarchial when i do set_index on a different dataframe. Not sure what I am missing here.
解决方案
Sort the dataframe should help
df.sort()