Pandas - 在 DataFrame 中的任何位置查找值的索引

Kem*_*eia 11 python pandas

我是 Python 和 Pandas 的新手。

我想security_id在我的 Pandas 数据框中找到某个值(假设)的索引,因为那是列开始的地方。(列上方有未知数量的行带有无关数据,左侧还有许多空“列”。)

据我所知,isin方法只返回一个关于值是否存在的布尔值,而不是它的索引。

我如何找到这个值的索引?

Rav*_*iam 17

获取所有列中匹配搜索词的行的索引

search = 'security_id' 
df.loc[df.isin([search]).any(axis=1)].index.tolist()
Run Code Online (Sandbox Code Playgroud)

筛选行以匹配所有列中的搜索词

search = 'search term' 
df.loc[df.isin([search]).any(axis=1)]
Run Code Online (Sandbox Code Playgroud)


Ujj*_*wal 2

假设您的 DataFrame 如下所示:

      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
Run Code Online (Sandbox Code Playgroud)

请执行下列操作 :

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
Run Code Online (Sandbox Code Playgroud)