2

I have a dataframe that currently looks like this:

country series year value 
usa     a      2010 21
usa     b      2015 22
usa     a      2017 23
usa     b      2010 22
usa     b      2017 23

aus     a      2010 21
aus     b      2015 22
aus     a      2017 23
aus     b      2010 22
aus     b      2017 23

When I run this code, it reduces the duplicity of the countries but not the series like I expect it to.

pop2.set_index(['Country','Series'])

I want:

country series year value 
usa     a      2010 21
               2017 23
        b      2010 22
               2015 22
               2017 23
aus     a      2010 21                  
               2017 23
        b      2010 22
               2015 22
               2017 23

Instead, it is returning:

country series year value 
usa     a      2010 21
        b      2015 22
        a      2017 23
        b      2010 22
        b      2017 23
aus     a      2010 21
        b      2015 22
        a      2017 23
        b      2010 22
        b      2017 23
2

1 Answer 1

1

There must be an index label for each row to display in a dataframe. Therefore, you need is a another level of index then you can show index "grouping" as you wish.

Let's try this:

df.set_index(['country','series',np.arange(df.shape[0])]).sort_index()

Output:

                  year  value
country series               
aus     a      5  2010     21
               7  2017     23
        b      6  2015     22
               8  2010     22
               9  2017     23
usa     a      0  2010     21
               2  2017     23
        b      1  2015     22
               3  2010     22
               4  2017     23
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.