多个groupby后如何将pandas数据从索引移动到列 [英] How to move pandas data from index to column after multiple groupby

查看:40
本文介绍了多个groupby后如何将pandas数据从索引移动到列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下熊猫数据框:

dfalph.head()令牌年使用书籍386 xanthos 1830 3 3387 xanthos 1840 1 1388 xanthos 1840 2 2389 xanthos 1868 2 2390 xanthos 1875 1 1

我用重复的 tokenyears 聚合行,如下所示:

dfalph = dfalph[['token','year','uses','books']].groupby(['token', 'year']).agg([np.sum])dfalph.columns = dfalph.columns.droplevel(1)dfalph.head()使用书籍象征年xanthos 1830 3 31840 3 31867 2 21868 2 21875 1 1

我想将它们返回到列并具有整数索引,而不是在索引中包含令牌"和年份"字段.

解决方案

方法#1:reset_index()

<预><代码>>>>G使用书籍总和象征年xanthos 1830 3 31840 3 31868 2 21875 1 1[4 行 x 2 列]>>>g = g.reset_index()>>>G令牌年使用书籍总和0 xanthos 1830 3 31 xanthos 1840 3 32 xanthos 1868 2 23 xanthos 1875 1 1[4 行 x 4 列]

方法 #2:首先不要使用 as_index=False

<预><代码>>>>g = dfalph[['token', 'year', 'uses', 'books']].groupby(['token', 'year'], as_index=False).sum()>>>G令牌年使用书籍0 xanthos 1830 3 31 xanthos 1840 3 32 xanthos 1868 2 23 xanthos 1875 1 1[4 行 x 4 列]

I have the following pandas dataframe:

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

I aggregate the rows with duplicate token and years like so:

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

Instead of having the 'token' and 'year' fields in the index, I would like to return them to columns and have an integer index.

解决方案

Method #1: reset_index()

>>> g
              uses  books
               sum    sum
token   year             
xanthos 1830     3      3
        1840     3      3
        1868     2      2
        1875     1      1

[4 rows x 2 columns]
>>> g = g.reset_index()
>>> g
     token  year  uses  books
                   sum    sum
0  xanthos  1830     3      3
1  xanthos  1840     3      3
2  xanthos  1868     2      2
3  xanthos  1875     1      1

[4 rows x 4 columns]

Method #2: don't make the index in the first place, using as_index=False

>>> g = dfalph[['token', 'year', 'uses', 'books']].groupby(['token', 'year'], as_index=False).sum()
>>> g
     token  year  uses  books
0  xanthos  1830     3      3
1  xanthos  1840     3      3
2  xanthos  1868     2      2
3  xanthos  1875     1      1

[4 rows x 4 columns]

这篇关于多个groupby后如何将pandas数据从索引移动到列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆