相关疑难解决方法(0)

如何将pandas数据帧的索引转换为列?

这似乎相当明显,但我似乎无法弄清楚如何将数据框的索引转换为列?

例如:

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)

python dataframe pandas

374
推荐指数
7
解决办法
44万
查看次数

如何在多个groupby之后将pandas数据从索引移动到列

我有以下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'字段,而是希望将它们返回到列并具有整数索引.

python multi-index pandas pandas-groupby

33
推荐指数
2
解决办法
3万
查看次数

标签 统计

pandas ×2

python ×2

dataframe ×1

multi-index ×1

pandas-groupby ×1