尝试通过标题获取数据框列时发生TypeError

时间:2019-11-18 14:40:25

标签: python pandas

我已经将sklearn的一个内置数据集加载到了pandas数据框中。 我想从该列标题指定的单个列中提取数据。 但是,这会导致类型错误,我无法弄清原因。 下方是MWE,我们将不胜感激。

import pandas as pd
from sklearn.datasets import load_breast_cancer
meh = load_breast_cancer()

X = pd.DataFrame(meh.data, columns=[meh.feature_names])

# Prints nice dataframe
print(X)  

# TypeError: only integer scalar arrays can be converted to a scalar index
print(X["mean radius"])

1 个答案:

答案 0 :(得分:2)

问题是您使用传递嵌套列表创建了MultiIndex,因此仅需删除[]

X = pd.DataFrame(meh.data, columns=meh.feature_names)

print(X.columns)
Index(['mean radius', 'mean texture', 'mean perimeter', 'mean area',
       'mean smoothness', 'mean compactness', 'mean concavity',
       'mean concave points', 'mean symmetry', 'mean fractal dimension',
       'radius error', 'texture error', 'perimeter error', 'area error',
       'smoothness error', 'compactness error', 'concavity error',
       'concave points error', 'symmetry error', 'fractal dimension error',
       'worst radius', 'worst texture', 'worst perimeter', 'worst area',
       'worst smoothness', 'worst compactness', 'worst concavity',
       'worst concave points', 'worst symmetry', 'worst fractal dimension'],
      dtype='object')

详细信息

X = pd.DataFrame(meh.data, columns=[meh.feature_names])
print(X.columns)

MultiIndex([(            'mean radius',),
            (           'mean texture',),
            (         'mean perimeter',),
            (              'mean area',),
            (        'mean smoothness',),
            (       'mean compactness',),
            (         'mean concavity',),
            (    'mean concave points',),
            (          'mean symmetry',),
            ( 'mean fractal dimension',),
            (           'radius error',),
            (          'texture error',),
            (        'perimeter error',),
            (             'area error',),
            (       'smoothness error',),
            (      'compactness error',),
            (        'concavity error',),
            (   'concave points error',),
            (         'symmetry error',),
            ('fractal dimension error',),
            (           'worst radius',),
            (          'worst texture',),
            (        'worst perimeter',),
            (             'worst area',),
            (       'worst smoothness',),
            (      'worst compactness',),
            (        'worst concavity',),
            (   'worst concave points',),
            (         'worst symmetry',),
            ('worst fractal dimension',)],
           )