У меня проблемы с пониманием того, почему вызов метода dataframe.apply для панд не возвращает ожидаемый результат. Может ли кто-нибудь пролить свет на то, почему первый вызов для применения, показанный ниже, не возвращает ожидаемый результат, а второй -?
import pandas as pd
import numpy as np
df = pd.DataFrame({
"x": [1, 2, np.nan],
"y": ["hi", "there", np.nan]
})
print(df)
#> x y
#> 0 1.0 hi
#> 1 2.0 there
#> 2 NaN NaN
print(df.dtypes)
#> x float64
#> y object
#> dtype: object
# why would something like this not return the expected result (which should
# be TRUE, FALSE):
print(df.apply(lambda x: np.issubdtype(x, np.number)))
#> x False
#> y False
#> dtype: bool
# but something like this returns the expected result (i.e., median imputation
# is used if the series is a number, otherwise NULLs are replaced with "MISSING"):
def replace_nulls(s):
is_numeric = np.issubdtype(s, np.number)
missing_value = s.median() if is_numeric else "MISSING"
return np.where(s.isnull(), missing_value, s)
print(df.apply(replace_nulls))
#> x y
#> 0 1.0 hi
#> 1 2.0 there
#> 2 1.5 MISSING
Создано в 2019 году-10-03 по представлению пакета