user10724844
user10724844

Reputation:

Groupby categorical variable

I have a data frame similar to this:

name     Gender    Country
John       1        US
Kingsley   1        US
Smith      0        UK
Lily       0        UK
Elizabeth  0        US
Elizabeth  1        US 
Lily       1        UK 
Smith      0        US

I am hoping to print two lists:

  1. Same Name, Different Gender e.g. Elizabeth 0 Elizabeth 1

  2. Same Name, Different Country e.g. Smith US Smith UK

I am using

df.groupby('name')['gender'] 

however, I wasn't able to get the list that I wanted, such as

df1
Elizabeth 0
Elizabeth 1
Lily 0 
Lily 1

Is there anyway to solve the code problem?

Upvotes: 1

Views: 98

Answers (1)

BENY
BENY

Reputation: 323326

Try with GroupBy.transform with nunqiue

s = df[df.groupby('name').Gender.transform('nunique').gt(1)]
Out[53]: 
        name  Gender Country
3       Lily       0      UK
4  Elizabeth       0      US
5  Elizabeth       1      US
6       Lily       1      UK

For 2nd part use the same logic as above

df[df.groupby('name').Country.transform('nunique').gt(1)]
    name  Gender Country
2  Smith       0      UK
7  Smith       0      US

Upvotes: 2

Related Questions