0

I'm trying to get data from column1 of a .xlsx file, when the data I provide matches the data in column2 and column4. I tried using data[data['column2'] == "aa1"] but not satisfied with the result. For column4 data, I used int(raw_input("get data: ")) but that didn't work either.

Sample excel file:

column1,column2,column3,column4
aa,aa1,lll,21
bb,aa2,ll,22

Required output:

aa #when an input of aa1 and 21 is given.

PS- The data is in the form of a pandas dataframe.

6
  • how are you loading the .xslx file into your python code? Commented Apr 15, 2015 at 7:34
  • "but not satisfied with the result" ? Commented Apr 15, 2015 at 7:35
  • 1
    @dbliss I did excel = pd.ExcelFile(file_name) df = excel.parse("Sheet1") Commented Apr 15, 2015 at 7:35
  • @AndyHayden It prints the complete row, when I just want data from column1. I tried suffixing with [0], that gave me a keyerror. Commented Apr 15, 2015 at 7:36
  • ok, so you're asking how to get data in a pandas DataFrame. please edit your question to include this. Commented Apr 15, 2015 at 7:37

1 Answer 1

1

You want this:

data[(data['column2'] == "aa1") & (data['column4'] == 21)]

So to use multiple conditions you need to use & instead of and because we are comparing arrays also you need to use parentheses due to operator precedence

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.