pandas用法

更全面的解说:

https://www.cnblogs.com/qi-yuan-008/p/12412018.html

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

代码如下所示:

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

df如下所示,以上通过选取“BoolCol”取值为3且“attr”取值为22的行,得到该行在df中的位置
注意:返回的位置为index列表,根据index的不同而不同,这点易于数组中默认的下标。


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

转载随意~:陶醉seo » pandas用法

赞 (0)
分享到:更多 ()