Suppose I have the following dataframe:
'a' 'b'
0 0 0
1 1 0
2 0 1
3 0 1
Is there a way I could get the index/column values for which a specific value exists? For example, something akin to the following:
values = df.search(1)
would have values = [(1, 'a'), (2, 'b'), (3, 'b')]
.
To find the indexes of the specific value that match the given condition in Pandas dataframe we will use df['Subject'] to match the given values and index. values to find an index of matched value. The result shows us that rows 0,1,2 have the value 'Math' in the Subject column.
By using loc and iloc We can access a single row and multiple rows of a DataFrame with the help of “loc” and “iloc”. Access a single row or multiple rows by name.
You can get the value of a cell from a pandas dataframe using df. iat[0,0] .
df[df == 1].stack().index.tolist()
yields
[(1, 'a'), (2, 'b'), (3, 'b')]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With