pandas.dataframe中根据条件获取元素所在的位置方法(索引)

今天小编就为大家分享一篇pandas.dataframe中根据条件获取元素所在的位置方法(索引),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

在dataframe中根据一定的条件,得到符合要求的某行元素所在的位置。

代码如下所示:

 df = pd.DataFrame({'BoolCol': [1, 2, 3, 3, 4],'attr': [22, 33, 22, 44, 66]}, index=[10,20,30,40,50]) print(df) a = df[(df.BoolCol==3)&(df.attr==22)].index.tolist() print(a) 

df如下所示,以上通过选取“BoolCol”取值为3且“attr”取值为22的行,得到该行在df中的位置

注意:返回的位置为index列表,根据index的不同而不同,这点易于数组中默认的下标。

 BoolCol attr 10  1 22 20  2 33 30  3 22 40  3 44 50  4 66 [30] 

以上就是pandas.dataframe中根据条件获取元素所在的位置方法(索引)的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » python