Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何通过比较列的值和不带ItError的字符串来获取dataframe的索引_Python_Pandas - Fatal编程技术网

Python 如何通过比较列的值和不带ItError的字符串来获取dataframe的索引

Python 如何通过比较列的值和不带ItError的字符串来获取dataframe的索引,python,pandas,Python,Pandas,我有一个包含大量行的数据帧(df): 0 A B C D E 1 ax 92 47 32 89 2 av 12 41 26 87 3 bn 54 87 98 09 string='av' 我需要找到A=av的df的索引 它需要返回2 我尝试使用df.iterrows(),但是有大量的行需要花费大量时间或崩溃 在没有iterrows的情况下可以这样做吗 index=df.loc

我有一个包含大量行的数据帧(df):

0   A     B     C     D     E
1   ax    92    47    32    89
2   av    12    41    26    87
3   bn    54    87    98    09
string='av' 我需要找到A=av的df的索引 它需要返回2

我尝试使用
df.iterrows()
,但是有大量的行需要花费大量时间或崩溃

在没有iterrows的情况下可以这样做吗

index=df.loc[df['A'] == 'av', '0'].iloc[0]
这种技术称为布尔屏蔽。索引的值为2
请参阅文件。熊猫是有据可查的

使用布尔过滤和
.loc

从列
'0'

In [377]: df.loc[df['A'] == 'av', '0']
Out[377]:
1    2
Name: 0, dtype: int64

In [378]: df.loc[df['A'] == 'av', '0'].item()
Out[378]: 2L

In [379]: df.loc[df['A'] == 'av', '0'].iloc[0]
Out[379]: 2