Search code examples
pythonpandasxlsximport-from-excel

Get data from one column based on another using Python


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.


Solution

  • 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