In [1]:
import pandas as pd
import seaborn as sns
In [2]:
df = sns.load_dataset('titanic')
df.head(2)
Out[2]:
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
In [3]:
df = df.loc[:, ['age', 'fare']]
df.head()
Out[3]:
age fare
0 22.0 7.2500
1 38.0 71.2833
2 26.0 7.9250
3 35.0 53.1000
4 35.0 8.0500

6.1. DataFrame을 받고 DataFrame을 리턴하는 함수 매핑 -> DataFrame 반환¶

In [4]:
def missing_value(x: pd.DataFrame) -> pd.DataFrame:
    return x.isnull()
In [5]:
result_df = df.pipe(missing_value)
result_df.head()
Out[5]:
age fare
0 False False
1 False False
2 False False
3 False False
4 False False

6.2. DataFrame을 받고 Series를 리턴하는 함수 매핑 -> Series 반환¶

In [6]:
def missing_count(x: pd.DataFrame) -> pd.Series:
    return x.isnull().sum()
In [7]:
result_series = df.pipe(missing_count)
result_series
Out[7]:
age     177
fare      0
dtype: int64

6.3. DataFrame을 받고 값을 리턴하는 함수 매핑 -> 값 반환¶

In [8]:
def total_number_missing(x: pd.DataFrame) -> int:
    return x.isnull().sum().sum()
In [9]:
result_value = df.pipe(total_number_missing)
result_value
Out[9]:
177