How to get index of all rows whose particular column satisfies given condition in Pandas
 Simple indexing operation to get the index of all rows whose particular column satisfies given condition

np.where()
method to get index of all rows whose particular column satisfies given condition 
pandas.DataFrame.query()
to get indices of all rows whose particular column satisfies given condition
We can get the index of all rows whose particular column satisfies given condition in Pandas using simple indexing operation. We could also find their indices using where()
method from NumPy
package and query()
method of DataFrame object.
Simple indexing operation to get the index of all rows whose particular column satisfies given condition
The use of simple indexing operation can accomplish the task of getting the index of rows whose particular column meets the given condition.
import pandas as pd
import numpy as np
dates=['April10', 'April11', 'April12', 'April13','April14','April16']
sales=[200,300,400,200,300,300]
prices=[3, 1, 2, 4,3,2]
df = pd.DataFrame({'Date':dates ,
'Sales':sales ,
'Price': prices})
reqd_Index = df[df['Sales']>=300].index.tolist()
print(reqd_Index)
Output:
[1, 2, 4, 5]
Here, df['Sales']>=300
gives series of boolean values whose elements are True
if their Sales
column has a value greater than or equal to 300.
We can retrieve the index of rows whose Sales
value is greater than or equal to 300 by using df[df['Sales']>=300].index
.
Finally, the tolist()
method converts all the indices to a list.
np.where()
method to get index of all rows whose particular column satisfies given condition
np.where()
takes condition as an input and returns the indices of elements that satisfy the given condition. Hence, we could use np.where()
to get indices of all rows whose particular column satisfies the given condition.
import pandas as pd
import numpy as np
dates=['April10', 'April11', 'April12', 'April13','April14','April16']
sales=[200,300,400,200,300,300]
prices=[3, 1, 2, 4,3,2]
df = pd.DataFrame({'Date':dates ,
'Sales':sales ,
'Price': prices})
reqd_Index = list(np.where(df["Sales"] >= 300))
print(reqd_Index)
Output:
[array([1, 2, 4, 5])]
This outputs indices of all the rows whose values in the Sales
column are greater than or equal to 300
.
pandas.DataFrame.query()
to get indices of all rows whose particular column satisfies given condition
pandas.DataFrame.query()
returns DataFrame resulting from the provided query expression. Now, we can use the index
attribute of DataFrame to return indices of all the rows whose particular column satisfies the given condition.
import pandas as pd
import numpy as np
dates=['April10', 'April11', 'April12', 'April13','April14','April16']
sales=[200,300,400,200,300,300]
prices=[3, 1, 2, 4,3,2]
df = pd.DataFrame({'Date':dates ,
'Sales':sales ,
'Price': prices})
reqd_index = df.query('Sales == 300').index.tolist()
print(reqd_index)
Output:
[1, 4, 5]
It returns the list of indices of all rows whose particular column satisfies the given condition Sales == 300
.