In [1]:
import pandas as pd
import seaborn as sns

df = sns.load_dataset('titanic')
print(df.shape)
df.head()
(891, 15)
Out[1]:
survived pclass sex age sibsp parch fare embarked class who adult_male deck embark_town alive alone
0 0 3 male 22.0 1 0 7.2500 S Third man True NaN Southampton no False
1 1 1 female 38.0 1 0 71.2833 C First woman False C Cherbourg yes False
2 1 3 female 26.0 0 0 7.9250 S Third woman False NaN Southampton yes True
3 1 1 female 35.0 1 0 53.1000 S First woman False C Southampton yes False
4 0 3 male 35.0 0 0 8.0500 S Third man True NaN Southampton no True

mask 로 먼저 확인¶

In [2]:
mask = (df['sibsp'] >= 3) & (df['sibsp'] <= 5)  # or나 and를 나타낼 때, `||` 나 `&&` 이 아니라, 한개씩
mask.value_counts()  # 39행
Out[2]:
False    852
True      39
Name: sibsp, dtype: int64
In [3]:
df_boolean = df.loc[mask, :]
print(df_boolean.shape)  # 39행
df_boolean.head()
(39, 15)
Out[3]:
survived pclass sex age sibsp parch fare embarked class who adult_male deck embark_town alive alone
7 0 3 male 2.0 3 1 21.0750 S Third child False NaN Southampton no False
16 0 3 male 2.0 4 1 29.1250 Q Third child False NaN Queenstown no False
24 0 3 female 8.0 3 1 21.0750 S Third child False NaN Southampton no False
27 0 1 male 19.0 3 2 263.0000 S First man True C Southampton no False
50 0 3 male 7.0 4 1 39.6875 S Third child False NaN Southampton no False

isin() 활용¶

In [4]:
isin_filter = df['sibsp'].isin([3, 4, 5])  # 3, 4, 5 에 해당하면 True
isin_filter.value_counts()  # 39행
Out[4]:
False    852
True      39
Name: sibsp, dtype: int64
In [5]:
df_isin = df.loc[isin_filter, :]
print(df_isin.shape)  # 39행
df_isin.head()  # boolean indexing 한거랑 동일
(39, 15)
Out[5]:
survived pclass sex age sibsp parch fare embarked class who adult_male deck embark_town alive alone
7 0 3 male 2.0 3 1 21.0750 S Third child False NaN Southampton no False
16 0 3 male 2.0 4 1 29.1250 Q Third child False NaN Queenstown no False
24 0 3 female 8.0 3 1 21.0750 S Third child False NaN Southampton no False
27 0 1 male 19.0 3 2 263.0000 S First man True C Southampton no False
50 0 3 male 7.0 4 1 39.6875 S Third child False NaN Southampton no False