python - Getting the integer index of a Pandas DataFrame row fulfilling a condition? -
i have following dataframe:
b c b 2 1 2 3 5 4 5 6
as can see, column b
used index. want ordinal number of row fulfilling ('b' == 5)
, in case 1
.
the column being tested can either index column (as b
in case) or regular column, e.g. may want find index of row fulfilling ('c' == 6)
.
you use np.where this:
import pandas pd import numpy np df = pd.dataframe(np.arange(1,7).reshape(2,3), columns = list('abc'), index=pd.series([2,5], name='b')) print(df) # b c # b # 2 1 2 3 # 5 4 5 6 print(np.where(df.index==5)[0]) # [1] print(np.where(df['c']==6)[0]) # [1]
the value returned array since there more 1 row particular index or value in column.
Comments
Post a Comment