python IndexError:布尔索引与维度0上的索引数组不匹配;维度为32,但相应的布尔维度为112

时间:2019-11-19 02:33:19

标签: python numpy

我是matploblib和numpy的新手,并且在尝试提取数据时遇到了问题。以下代码导致IndexError:布尔索引与维度0上的索引数组不匹配;维度是32,但相应的布尔维度是112。请指教!!

使用的数据集:https://data.gov.sg/dataset/monthly-motor-vehicle-population-by-type-of-fuel-used

import numpy as np
import matplotlib.pyplot as plt
title = "motor-vehicle-population-statistics-by-type-of-fuel-used."
titlelen = len(title)
print("{:*^{titlelen}}".format(title, titlelen=titlelen+6))
print()
data = np.genfromtxt("data/motor-vehicle-population-statistics-by-type-of-fuel-used.csv",
                      dtype=("datetime64[Y]","U100","U110",int),
                      delimiter=",",
                      names=True)

years = np.unique(data["month"])
category = np.unique(data['category'])
type = np.unique(data['type'])

cars = data[data["category"]=="Cars"]["number"]
carspetrol = cars[data["type"]=="Petrol"]["number"]
# print(cars)
print(carspetrol)

1 个答案:

答案 0 :(得分:0)

这里的问题很少。

第一个不要将python关键字用作变量 更改为

type = np.unique(data['type'])

这个

types = np.unique(data['type'])

您的错误是您正在尝试将具有112个值(数据)的布尔数组与32个元素的数组(汽车)进行比较。因此您的代码应像这样更改

cars = data[data["category"]=="Cars"]
carspetrol = cars[cars["type"]=="Petrol"]["number"]

与numpy相比,最好使用Pandas之类的分析库进行基本分析。

相关问题