在Python中删除常量列和某些列名的替代方法

2024-04-27 18:24:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用下面的代码删除常量列和带有某些标题的列。在

有没有更像Python的方法?在

import numpy as np
import pandas as pd
from sklearn.datasets import make_classification
from sklearn.tree import DecisionTreeClassifier

X, y = make_classification(n_samples=1000,
                           n_features=6,
                           n_informative=3,
                           n_classes=2,
                           random_state=0,
                           shuffle=False)

# Creating a dataFrame
df = pd.DataFrame({'car':X[:,0],
                                  'ball':X[:,1],
                                  'Feature 3': 5,
                                  'Feature 4':X[:,3],
                                  'Feature 5':X[:,4],
                                  'Feature 6':X[:,5],
                                  'Class':y})
one = df.std().eq(0).reindex(df.columns, fill_value=True)
two = one.index.str.contains("ball|car")
all = one| two


df_auto = df.loc[:, ~all].copy()

Tags: 代码fromimportdfmakeassklearnall
1条回答
网友
1楼 · 发布于 2024-04-27 18:24:39

我看不出你目前的逻辑有什么明显的问题。”“Python”是主观的,我在下面提供一个不同的解决方案。在

这是一种可选的基于numpy+.iloc的方法,您可能更喜欢:

n1 = np.where(np.std(df.values, axis=0) == 0)[0]
n2 = np.where(df.columns.str.contains('ball|car'))[0]

df_auto = df.iloc[:, np.delete(range(len(df.columns)), np.hstack((n1, n2)))].copy()

相关问题 更多 >