aysh
aysh

Reputation: 599

ValueError: Item wrong length 0 instead of 150. while extracting the values from dataframe

Data set is below

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

Code is below

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] has to print return the dataframe if Sep Wid(X[:1]) > 4

I am getting error ValueError: Item wrong length 0 instead of 150.

Upvotes: 1

Views: 1720

Answers (1)

jezrael
jezrael

Reputation: 863226

I think problem is with selecting second 'column' - need : for all rows, then comma and 4 for select last fifth column:

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']

Or seelct last column:

X = df.iloc[:,-1].values

Similar for 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]]

Upvotes: 1

Related Questions