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

如何用Pandas库实现输入参赛者返回对应参赛类别的功能?

用Pandas实现参赛者类别查询功能

核心实现步骤

1. 确认CSV字段结构

先通过df.columns查看你的CSV文件包含的所有列名,明确存储参赛类别的列名称(比如列名为参赛类别),这是提取目标数据的关键。

2. 基础单条件查询实现

以下是根据用户输入的单一信息(如姓名)查询参赛类别的示例代码:

import pandas as pd

# 读取CSV文件(替换为你的实际文件路径)
df = pd.read_csv('竞赛数据.csv')

# 获取用户输入
user_input = input("请输入参赛者姓名:")

# 筛选匹配的行
match_result = df[df['姓名'] == user_input]

# 提取并输出参赛类别
if not match_result.empty:
    # 取第一行的参赛类别(假设姓名唯一)
    category = match_result['参赛类别'].iloc[0]
    print(f"参赛类别:{category}")
else:
    print("未找到该参赛者信息")

3. 多条件查询(可选)

如果需要结合多个信息(如姓名+年龄)进行精准匹配,可使用组合条件:

import pandas as pd

df = pd.read_csv('竞赛数据.csv')

name_input = input("请输入参赛者姓名:")
age_input = int(input("请输入参赛者年龄:"))

# 用&连接多个条件,注意条件要加括号
match_result = df[(df['姓名'] == name_input) & (df['年龄'] == age_input)]

if not match_result.empty:
    category = match_result['参赛类别'].iloc[0]
    print(f"参赛类别:{category}")
else:
    print("未找到匹配的参赛者信息")

4. 大数据量优化(可选)

如果CSV数据量较大,可将常用查询列设为索引提升查询效率:

import pandas as pd

df = pd.read_csv('竞赛数据.csv')
# 将姓名列设为索引
df.set_index('姓名', inplace=True)

user_input = input("请输入参赛者姓名:")
if user_input in df.index:
    category = df.loc[user_input, '参赛类别']
    print(f"参赛类别:{category}")
else:
    print("未找到该参赛者信息")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 06:02:05