pandas - ValueError:项目错误长度 0 而不是 150。同时从数据框中提取值
问题描述
数据集如下
5.1,3.5,1.4,0.2,setosa
4.9,3.0,1.4,0.2,setosa
4.7,3.2,1.3,0.2,setosa
4.6,3.1,1.5,0.2,setosa
代码如下
df = pd.read_csv('iris.data.csv',header=None,sep=',')
df.columns = ['Sep Len','Sep Wid','Pet Len','Pet Wid','Species']
X = df.iloc[:0:4].values
y = df.iloc[:4].values
Sepal_Width = X[:1]
iris_outliers = Sepal_Width > 4
df[iris_outliers]
df[iris_outliers]
必须打印return the dataframe if Sep Wid(X[:1]) > 4
我收到错误 ValueError: Item wrong length 0 而不是 150。
解决方案
我认为问题在于选择第二个'column'
- 需要:
所有行,然后是逗号并4
选择最后第五列:
cols = ['Sep Len','Sep Wid','Pet Len','Pet Wid','Species']
df = pd.read_csv('iris.data.csv', names=cols)
X = df.iloc[:,4].values
print (X)
['setosa' 'setosa' 'setosa' 'setosa']
或查看最后一栏:
X = df.iloc[:,-1].values
类似y
:
y = df.iloc[:,:4].values
y = df.iloc[:, :-1].values
print (y)
[[5.1 3.5 1.4 0.2]
[4.9 3. 1.4 0.2]
[4.7 3.2 1.3 0.2]
[4.6 3.1 1.5 0.2]]
推荐阅读
- .net - 如何将复选框与arraylist绑定?
- reactjs - 客户端渲染在具有样式的浏览器客户端中对 html 做出反应
- python-3.x - 为什么会产生这个 no assembly found 的错误?master.extpython
- python-3.x - 即使没有应用前向传播,也会调整 nn.ModuleList() 的权重
- node.js - TSLoader + Babel Polyfill :在运行 npm run build 时,您可能需要一个额外的加载器来处理这些加载器的结果
- matlab - 如何在没有循环的情况下在 Matlab 数组中的特定索引处输入元素?
- python - 如何在 django 项目中运行 Jupyter Notebook 代码
- android - 基于 JWT 的 Web 应用和原生 android 应用的身份验证
- vba - 搜索空单元格;将相邻单元格复制到消息框;删除行或将文本添加到空单元格
- jquery - 如何获取最接近可滚动 div 的可视区域顶部的 div 的索引?