Reputation: 171
I have a dataframe (multiple columns) and I want to find the exact (numerical) location in the dataframe of certain strings. With location I mean I want to know both it's row number as well as it's column number (not column name).
Let's make an example with the following dataframe df
:
hi apple fine
right yes okay
maybe apple no
I want to find the location of all apples so I want to know there is an apple
on row 0 column 1 and an apple
on row 2 column 1.
How can I find these numbers?
I started with looking for all places where the condition is fulfilled that that element contains indeed the string I'm looking for, with:
df[df=='apple']
This is fine but how do I get the location of all True
?
I tried this df.index[df=='apple'].tolist()
but it gives an error because there are multiple columns. I understand I can solve this by running a for loop over all columns but wondering if there is something more straightforward. Also str.find()
doesn't give me the column number. I also looked into get_loc
but this only seemed to work for indices.
Upvotes: 1
Views: 706
Reputation: 195543
You can use np.argwhere
:
print(np.argwhere(df.values == "apple"))
Prints:
[[0 1]
[2 1]]
As a list:
print(np.argwhere(df.values == "apple").tolist())
Prints:
[[0, 1], [2, 1]]
Upvotes: 2