python - 给定的列不是数据框 Pandas 的列
问题描述
我有以下拆分功能:
from typing import Tuple
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.model_selection import train_test_split
def split_dataframe(
df: pd.DataFrame,
target_feature: str,
split_ratio: int = 0.2
) -> Tuple[pd.DataFrame, pd.DataFrame, np.ndarray, np.ndarray]:
df_ = df.copy()
X = df_.drop(target_feature, axis=1)
y = df_[target_feature]
encoder = LabelEncoder()
y = encoder.fit_transform(y)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = split_ratio)
return X_train, X_test, y_train, y_test
我使用以下方法拆分数据框:
X_train, X_test, y_train, y_test = split_dataframe(df, 'Банк')
我使用管道来转换 X_train 和 y_train
from sklearn.pipeline import Pipeline, FeatureUnion
from mlxtend.feature_selection import ColumnSelector
import category_encoders as ce
cat_pipe = Pipeline(
[
('selector', ColumnSelector(categorical_features)),
('encoder', ce.one_hot.OneHotEncoder())
]
)
num_pipe = Pipeline(
[
('selector', ColumnSelector(numeric_features)),
('scaler', StandardScaler())
]
)
preprocessor = FeatureUnion(
transformer_list=[
('cat', cat_pipe),
('num', num_pipe)
]
)
new_df = pipe.fit_transform(X_train, y_train)
之后我得到了ValueError: A given column is not a column of the dataframe,特别是KeyError: 'Банк'。我检查了在传递数据帧之前是否存在列以在训练和测试中拆分。如果我删除一切正常X = df_.drop(target_feature, axis=1)
,X = df_
但目标功能仍在 X 中。
解决方案
我发现了一个错误pipe.fit_transform(X_train, y_train)
,我将其更改为preprocessor.fit_transform(X_train, y_train)
并且它有效
推荐阅读
- python - 如何从已删除的消息中获取嵌入内容?
- amazon-web-services - 如何检查 AWS ECR 身份验证令牌是否未过期?
- i18next - i18n + vue3,路由器集成不起作用
- kubernetes - 我可以拍摄使用 kubernetes.io/aws-ebs 配置的 pv pvc 的快照吗?
- django - 更改移动绘图图
- amazon-web-services - SSL 证书错误:在 Windows 10 中使用 aws 客户端时 [SSL: CERTIFICATE_VERIFY_FAILED]
- wordpress - 首次登录后自动分配新的 user_email 并限制更新
- python-3.x - 如何使用 np.where/np.select 根据某些条件更新 pandas 数据框列中的各个字典?
- javascript - Typescript 不允许动态访问属性
- pandas - 使用 jupyterlab 在 Jupiter notebook 中使用 read_csv 时如何使用 tqdm 显示进度条