Python 如何通过比较列的值和不带ItError的字符串来获取dataframe的索引
我有一个包含大量行的数据帧(df):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
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