【发布时间】:2018-03-29 19:02:29
【问题描述】:
我正在对包含数字列的数据框执行 min-max-scaler 操作,但如果在这些数字列中,如果任何单元格包含字符串或空值,那么我会遇到异常。 为了避免这种情况,我想将字符串或空单元格转换为 0。 如何执行? 我的功能:
def min_max_scaler(df_sub,col_names):
"""
import the following:
from sklearn import preprocessing
from sklearn.preprocessing import StandardScaler
df_sub : Expecting a subset of data frame in which every columns should be number fields
(It contains all the columns on which you want to perform the operation)
example : df_subset = df.filter(['latitude','longitude','order.id'], axis=1)
col_names : All column names of the subset
"""
scaler = preprocessing.MinMaxScaler()
scaled_df = scaler.fit_transform(df_sub)
scaled_df = pd.DataFrame(scaled_df, columns=col_names)
return scaled_df
数据集:
day phone_calls received
7 180 NaN
8 8 NaN
9 -240 qbb
如何在执行此功能之前进行验证。请帮助。
【问题讨论】:
标签: python pandas scikit-learn