I found the answer its test2 = test_pd.groupby(by = ['ID'])['country','color'].nunique().reset_index()
idk why this question was marked as duplicate when the link provided by rafael didnt answer the question
I have a data frame with 3 columns:
country color ID
0 Germany Red 12
1 France Red 13
2 US Blue 11
3 France Red 11
If I want to find out the number of distinct countries and colors per ID in SQL it would be
select ID
, count(distinct(country)) as num_countries
, count(distinct(color)) as num_color
from table_name
group by ID;
The result will look like this
ID num_countries num_color
0 12 1 1
0 11 2 2
0 13 1 1
How do I achieve the same result in Pandas?