gpt4 book ai didi

python - 仅对数据框中的非数字列进行编码

转载 作者:太空宇宙 更新时间:2023-11-03 21:17:49 24 4
gpt4 key购买 nike

我有一个包含数字和非数字列的数据框。我只想对非数字列进行编码,并保持数字列的值不变。当我尝试使用我的代码进行编码时,它会对所有列进行编码。

您能帮忙解决这个问题吗?

谢谢

这是我的Python代码

from sklearn.base import BaseEstimator
from sklearn.base import TransformerMixin
from sklearn.preprocessing import LabelEncoder

class CustomImputer(BaseEstimator, TransformerMixin):
def __init__(self, strategy='mode',filler='NA'):
self.strategy = strategy
self.fill = filler

def fit(self, X, y=None):
if self.strategy in ['mean','median']:
if not all(X.dtypes == np.number):
raise ValueError('dtypes mismatch np.number dtype is \
required for '+ self.strategy)
if self.strategy == 'mean':
self.fill = X.mean()
elif self.strategy == 'median':
self.fill = X.median()
elif self.strategy == 'mode':
self.fill = X.mode().iloc[0]
elif self.strategy == 'fill':
if type(self.fill) is list and type(X) is pd.DataFrame:
self.fill = dict([(cname, v) for cname,v in zip(X.columns, self.fill)])
return self

def transform(self, X, y=None):
return X.fillna(self.fill)

data3=CustomImputer(strategy='mode').fit_transform(data2)

最佳答案

您正在寻找X.select_dtypes(np.number)

关于python - 仅对数据框中的非数字列进行编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54547521/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com