这似乎相当明显,但我似乎无法弄清楚如何将数据框的索引转换为列?
例如:
df=
gi ptt_loc
0 384444683 593
1 384444684 594
2 384444686 596
Run Code Online (Sandbox Code Playgroud)
至,
df=
index1 gi ptt_loc
0 0 384444683 593
1 1 384444684 594
2 2 384444686 596
Run Code Online (Sandbox Code Playgroud) 我有以下pandas数据帧:
dfalph.head()
token year uses books
386 xanthos 1830 3 3
387 xanthos 1840 1 1
388 xanthos 1840 2 2
389 xanthos 1868 2 2
390 xanthos 1875 1 1
Run Code Online (Sandbox Code Playgroud)
我使用重复令牌聚合行,并且如下所示:
dfalph = dfalph[['token','year','uses','books']].groupby(['token', 'year']).agg([np.sum])
dfalph.columns = dfalph.columns.droplevel(1)
dfalph.head()
uses books
token year
xanthos 1830 3 3
1840 3 3
1867 2 2
1868 2 2
1875 1 1
Run Code Online (Sandbox Code Playgroud)
我没有在索引中使用'token'和'year'字段,而是希望将它们返回到列并具有整数索引.