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?

12
  • 2
    What is the result of print(df.columns)? Commented Feb 17, 2017 at 11:45
  • Try this: df = pd.read_excel('file.xlsx', header=[1]) Commented Feb 17, 2017 at 11:46
  • It gives the same result: KeyError: 'apples' Commented Feb 17, 2017 at 11:47
  • When I tried this one, "TypeError: list indices must be integers, not list" error occurs @MaxU Commented Feb 17, 2017 at 11:49
  • 1
    @OykuA, do you expect people to type your data manually from that screenshot? ;-) Commented Feb 17, 2017 at 12:10

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.