Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Data Science by (50.2k points)

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.

1 Answer

0 votes
by (108k points)

You don't create the index in the first place, just use as_index=False

>>> a = dfalph[['token', 'year', 'uses', 'books']].groupby(['token', 'year'], as_index=False).sum()

>>> a

     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]

If you wish to learn more about Pandas visit this Pandas Tutorial.

Browse Categories

...