Pandas - find index of value anywhere in DataFrame

Get the index for rows matching search term in all columns

search = 'security_id' 
df.loc[df.isin([search]).any(axis=1)].index.tolist()

Rows filtered for matching search term in all columns

search = 'search term' 
df.loc[df.isin([search]).any(axis=1)]

I think this question may have been asked before here. The accepted answer is pretty comprehensive and should help you find the index of a value in a column.

Edit: if the column that the value exists in is not known, then you could use:

for col in df.columns:
    df[df[col] == 'security_id'].index.tolist()

Supposing that your DataFrame looks like the following :

      0       1            2      3    4
0     a      er          tfr    sdf   34
1    rt     tyh          fgd    thy  rer
2     1       2            3      4    5
3     6       7            8      9   10
4   dsf     wew  security_id   name  age
5   dfs    bgbf          121  jason   34
6  dddp    gpot         5754   mike   37
7  fpoo  werwrw          342   jack   31

Do the following :

for row in range(df.shape[0]): # df is the DataFrame
         for col in range(df.shape[1]):
             if df.get_value(row,col) == 'security_id':
                 print(row, col)
                 break

A oneliner solution avoiding explicit loops...

  • returning the entire row(s)

    df.iloc[np.flatnonzero((df=='security_id').values)//df.shape[1],:]

  • returning row(s) and column(s)

    df.iloc[ np.flatnonzero((df=='security_id').values)//df.shape[1], np.unique(np.flatnonzero((df=='security_id').values)%df.shape[1]) ]

Tags:

Python

Pandas