acg
acg

Reputation: 513

Find single dataframe row index matching 2 columns conditionals

I have a dataframe with 3 columns and I need to get the index of the row that match the values with 2 columns.

For example, the dataframe below:

Name     City   Country
Pietro   Roma   Italy
Maria    Milan  Italy
Pietro   NY     USA

In this case, I need to get the index of Pietro|Roma|Italy, seaching for Name and City columns only.

I tried doing the code below, but it returns all the rows that matches the 2 columns.

idx = np.where(dataframe[dataframe["Name"] == 'Pietro'],dataframe[dataframe["City"] == 'Roma'])

But it returns an array of indexes [[0,2],[0]] and I need to return index 0 that is where I have Name = 'Pietro' and City = 'Roma'

Updated with Solution

The solution is:

dataframe.index[(dataframe["Name"] == 'Pietro')&(dataframe["City"] == 'Roma')][0]

Upvotes: 1

Views: 44

Answers (1)

BENY
BENY

Reputation: 323246

Using

dataframe.index[(dataframe["Name"] == 'Pietro')&(dataframe["City"] == 'Roma')]

Upvotes: 4

Related Questions