如何使用MultiIndex.from_product重新索引?

时间:2016-02-22 09:18:35

标签: python pandas

源数据:

data = pandas.DataFrame(
[["bainuo","pay",2418,2811,0],
["ctrip_distributor","pay",468,613,0],
["bainuo","refund",855,1001,231],
["ctrip_distributor","refund",195,264,55],
["dianpinghotel","refund",53,60,52],
["bainuo","check_in",1705,1888,184],
["ctrip_distributor","check_in",264,313,73],
["dianpinghotel","check_in",5,5,4]],columns=["partner","state","oid","quantity","roomnight"])

我想:

data = pandas.pivot_table(data,values=['oid','quantity','roomnight'],index=['partner'],columns=['state'])
data = data.reorder_levels([1,0],axis=1).reindex(columns=['oid','quantity','roomnight'],level=1).reindex(columns=['pay','refund','check_in'],level=0).fillna(0)

它有效 但是当我尝试另一种方式时:

data = pandas.pivot_table(data,values=['oid','quantity','roomnight'],index=['partner'],columns=['state'])
idx = pandas.MultiIndex.from_product([['pay','refund','check_in'],['oid','quantity','roomnight']])
data = data.reindex(columns=idx)

结果错了!

1 个答案:

答案 0 :(得分:2)

在第二种情况下,您首先需要执行.reorder_levels([1,0],axis=1),然后使用MultiIndex的reindex将起作用:

data.reorder_levels([1,0],axis=1).reindex(columns=idx)

原因是reindex无法交换级别,它只会尝试匹配级别内的标签,因此您可以获得所有NaN。