Pandas how to find column contains a certain value
In this tutorial, we will learn how to find the index of a column that contains a certain value.
Example we have a dataframe with the following columns:
import pandas as pd
df = pd.DataFrame({
'A': [1, 4, 7, 1, 4],
'B': [2, 5, 8, 2, 5],
'C': [3, 6, 9, 3, 6]
})
A B C
0 1 2 3
1 4 5 6
2 7 8 9
4 1 2 3
5 4 5 6
Let us find the index of the column that contains the value 5
.
Using np.where from numpy
The numpy function np.where
can be used to find the index of a column that contains a certain value.
The syntax is:
numpy.where(condition[, x, y])
Parameters
conditionarray_like, bool
Where True, yield x, otherwise yield y.
x, y: array_like
Values from which to choose. x, y and condition need to be broadcastable to some shape.
Returns
out: ndarray
An array with elements from x where condition is True, and elements from y elsewhere.
For the above example, we can use the following code to find the index of the column B
that contains the value 5
.
import numpy as np
col_index = pd.DataFrame(np.where(df.eq(5))[1] + 1, columns=['col_index'])
Output:
col_index
0 2
1 2