2

I am trying to read and filter from excel data in Python. I used the code below:

import pandas as pd
import numpy as np
df = pd.read_excel('file.xlsx') 
df['apples'] = (pd.cut(df['apples'],bins=[-np.inf,2,5,np.inf],labels=['WOW','ok','BOB']))
print(df)

This is my excel file

But KeyError: 'apples' occurs. Do you have any advice about how can I fix this?

OykuA
  • 81
  • 3
  • 14

2 Answers2

1

Do you also want to modify the xlsx file? Or you just want to read it and apply some code to it? In the second case you could do:

df = df.drop(['apples'])

And you can input:

inputX = df.loc[:, ['oranges', 'lemons']].as_matrix()

It depends what do you want to do with it.

CrisH
  • 280
  • 1
  • 10
1

There is problem you have header with 2 rows, so by default columns of DataFrame are created by first row.

So need skip this first row by:

df = pd.read_excel('file.xlsx', skiprows=1)

Or:

df = pd.read_excel('file.xlsx', header=1)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252