python pandas查找DataFrame中指定数据的索引

举个例子,代码演示:

1,创建一个DataFrame

import pandas as pd
df = pd.DataFrame({'BoolCol': [10, 20, 30, 30, 40],'attr': [22, 33, 22, 44, 66]},index=[1,2,3,4,5])
print(df)
"""
   BoolCol  attr
1       10    22
2       20    33
3       30    22
4       30    44
5       40    66
"""

2,寻找数据中columns列名 BoolCol=30attr=44 所在的行索引

# 方法1:
value_result = df[(df.BoolCol==30)&(df.attr==44)].index.tolist()
print(value_result)
"""
[4]
"""

# 方法2:
value_result = df.loc[(df['BoolCol']==30)&(df['attr']==44),['BoolCol','attr']]
# 后面的['BoolCol','attr']是要显示的指定columns数据
print(value_result)
"""
   BoolCol  attr
4       30    44
"""

3,返回列表中的4就是满足条件的索引

  • 7
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值