Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

I know that I can get the unique values of a DataFrame by resetting the index but is there a way to avoid this step and get the unique values directly?

Given I have:

        C

 A B     

 0 one  3

 1 one  2

 2 two  1

I can do:

df = df.reset_index()

uniq_b = df.B.unique()

df = df.set_index(['A','B'])

Is there a way built in pandas to do this?

1 Answer

0 votes
by (41.4k points)

Use index.levels:

In [11]: df

Out[11]: 

       C

A B     

0 one  3

1 one  2

2 two  1

In [12]: df.index.levels[1]

Out[12]: Index([one, two], dtype=object)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...