BUG: DataFrame.stack sorts columns · Issue #53786 · pandas-dev/pandas (original) (raw)
mi = MultiIndex(
levels=[["foo", "bar"], ["one", "two"], ["a", "b"]],
codes=[[0, 0, 1, 1], [0, 1, 0, 1], [1, 0, 1, 0]],
names=["first", "second", "third"],
)
s = Series(0, index=mi)
unstacked = s.unstack([1, 2])
print(unstacked)
# second one two
# third b a
# first
# foo 0 0
# bar 0 0
result = unstacked.stack(0)
print(result)
# third a b
# first second
# foo one NaN 0.0
# two 0.0 NaN
# bar one NaN 0.0
# two 0.0 NaN
The expected result should have the columns b
and then a
, the order in the original DataFrame.