简体   繁体   中英

LabelEncoder within Lambda function

I'm working with the Ames, Iowa housing data, and I'd like to use a LabelEncoder within a lambda function to label encode my string values in my categorical features while skipping over the NaN values (so I can impute them later). This is what I have so far:

train['Fireplace Qu'].apply(lambda x: LabelEncoder(x).fit_transform if type(x) != np.float else x)

But it throws this error:

TypeError: object() takes no parameters

Any help would be greatly appreciated - trying to figure out a way to impute categorical data.

Let us using factorize

pd.Series(pd.factorize(df.group)[0]).replace(-1,np.nan)
Out[141]: 
0    NaN
1    NaN
2    0.0
3    0.0
4    NaN
5    NaN
6    NaN
7    NaN
8    1.0
dtype: float64

Or

df.loc[df.group.notnull(),'group']=df.group.astype('category').cat.codes

Data input

  group
0   NaN
1   NaN
2     a
3     a
4   NaN
5   NaN
6   NaN
7   NaN
8     b

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM