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

Pandas筛选指定国家员工遇索引越界,修改后仍无数据求助

筛选DataFrame中指定国家员工时的索引错误及无结果问题

原始代码

import pandas as pd
import matplotlib.pyplot as plt
import requests
from bs4 import BeautifulSoup

your_origin_country = 'China'

employee_data = df
employee_data

selected_employee = employee_data[employee_data['origin_country'] == your_origin_country].iloc[0]

数据源(DataFrame)

first_namelast_nameemailgenderpositionsalaryorigin_country
TawnyaWithefordtwitheford0@seattletimes.comFemaleTechnical Architect58286Russia
AnnaWithefordtwitheford0@seattletimes.comFemaleTechnical Architect58286Mexico
AnnieWithefordtwitheford0@seattletimes.comFemaleTechnical Architect58286China

运行报错

IndexError                                Traceback (most recent call last)
 in <cell line: 14>()
12 # Assuming you have a DataFrame named 'employee_data' with columns like 'origin_country', 'employee_id'
13 # Replace 'employee_data' with your actual DataFrame
---&gt; 14 selected_employee = employee_data[employee_data['origin_country'] == your_origin_country].iloc[0,:]

4 frames
/usr/local/lib/python3.10/dist-packages/pandas/core/indexing.py in _validate_integer(self, key, axis)
1555         len_axis = len(self.obj._get_axis(axis))
1556         if key &gt;= len_axis or key &lt; -len_axis:
-&gt; 1557             raise IndexError("single positional indexer is out-of-bounds")
1558
1559     # -------------------------------------------------------------------

IndexError: single positional indexer is out-of-bounds

修改后的代码

import pandas as pd
import matplotlib.pyplot as plt
import requests
from bs4 import BeautifulSoup

# Step 1: Select an Employee
# Replace 'your_origin_country' with the desired country
your_origin_country = 'Canada'

employee_data = df
employee_data
# Assuming you have a DataFrame named 'employee_data' with columns like 
'origin_country', 'employee_id'
# Replace 'employee_data' with your actual DataFrame
selected_employee = employee_data[employee_data['origin_country'] == your_origin_country].reset_index()
selected_employee.head(1)

问题说明

原本希望筛选出国籍为'China'的员工,运行原始代码时触发索引越界错误。修改代码后,得到的结果是空表头且无数据,需要解决该问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 04:00:13