Post

python에서 그룹별 범주 구하기

python에서 그룹별 유일한(unique)한 범주를 구하는 방법을 알아본다.

python에서 그룹별 범주 구하기

python에서 그룹별 범주 구하기

groupby()unique() 함수를 통해 그룹별 범주형 컬럼에서 유일한 값을 계산한다.

1
2
3
4
5
import pandas as pd
import seaborn as sns
penguins = sns.load_dataset('penguins')

1
2
3
4
df = penguins.copy()
df.groupby(['species'])['island'].unique()

1
2
3
4
5
species
Adelie       [Torgersen, Biscoe, Dream]
Chinstrap                       [Dream]
Gentoo                         [Biscoe]
Name: island, dtype: object
1
df.groupby(['species'])['island'].nunique()
1
2
3
4
5
species
Adelie       3
Chinstrap    1
Gentoo       1
Name: island, dtype: int64

R에서 그룹별 범주 구하기

1
2
3
4
5
library(dplyr)
df <- palmerpenguins::penguins
df %>% 
  group_by(species) %>% 
  reframe(categories = unique(island))
1
2
3
4
5
6
7
8
# A tibble: 5 × 2
  species   categories
<fct>     <fct>     
1 Adelie    Torgersen 
2 Adelie    Biscoe    
3 Adelie    Dream     
4 Chinstrap Dream     
5 Gentoo    Biscoe 
1
2
3
df %>% 
  group_by(species) %>% 
  reframe(categories = n_distinct(island))
1
2
3
4
5
6
# A tibble: 3 × 2
  species   categories
  <fct>          <int>
1 Adelie             3
2 Chinstrap          1
3 Gentoo             1

Reference

  1. https://pandas.pydata.org/docs/reference/api/pandas.unique.html
This post is licensed under CC BY 4.0 by the author.