You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Pandas DataFrame中查找指定文本的行位置

解决方法

错误原因

你报错是因为搞混了列表和pandas Series的API:info_day.index是Series的索引属性(返回RangeIndex对象),不是像列表那样用来查找值位置的index()方法,所以直接调用会提示“对象不可调用”。

方案1:用get_loc快速定位唯一值

如果确定today-4.txt在列中只出现一次,用Series的get_loc()方法最直接:

matched_index = info_day.get_loc('today-4.txt')
# 结果:3

方案2:布尔筛选取所有匹配索引

如果目标值可能重复出现,用布尔筛选可以获取所有匹配的行索引:

# 筛选出值等于目标的行,提取其索引
matched_indices = info_day[info_day == 'today-4.txt'].index
# 取第一个匹配的索引(如果唯一)
matched_index = matched_indices[0]

方案3:直接在原DataFrame操作(更高效)

不需要单独提取列,直接在原DataFrame上筛选,还能同时拿到该行的其他数据:

# 筛选出目标行
target_row = filesday[filesday.iloc[:, 2] == 'today-4.txt']
# 获取行索引
matched_index = target_row.index[0]
# 提取该行第3列的数据(比如示例中的orange)
target_data = target_row.iloc[0, 3]

内容的提问来源于stack exchange,提问作者iTanish

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 06:35:48