Sir*_* S. 1 python transpose hierarchical-data dataframe pandas
下面是我的数据框架,具有两级索引。我希望“仅”将外部索引转置为列。我想要的输出将是2X2数据帧,而不是现在的4X1数据帧。你们任何人能帮忙吗?
0
0 0 232
1 3453
1 0 443
1 3241
Run Code Online (Sandbox Code Playgroud)
有了多重索引,您可以在级别0上使用unstack()。
import pandas as pd
import numpy as np
index = pd.MultiIndex.from_tuples([(0,0),(0,1),(1,0),(1,1)])
df = pd.DataFrame([[1],[2],[3],[4]] , index=index, columns=[0])
print df.unstack(level=[0])
0
0 1
0 1 3
1 2 4
Run Code Online (Sandbox Code Playgroud)