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

初学者求助:如何从pandas Series中提取纯字符串值

提取pandas Series中的纯字符串值
  • 若Series仅含单个元素,用item()方法直接获取原始标量值,这是最简洁的方式:
    import pandas as pd
    s = pd.Series(["Gear"])
    pure_string = s.item()
    # pure_string 即为 "Gear"
    
  • 如果通过索引取值时得到带括号的对象,说明Series存储的是元组类元素(比如pd.Series([('Gear',)], index=['col'])),可以直接提取元组内的元素:
    # 方式1:直接索引元组
    pure_string = s.loc['col'][0]
    # 方式2:用str属性提取
    pure_string = s.str[0].loc['col']
    
  • 也可以用iloc[0]直接获取单元素Series的第一个值:
    pure_string = s.iloc[0]
    

以上方法都能绕过str()转换带来的多余字符,直接拿到纯字符串内容。

内容的提问来源于stack exchange,提问作者Cristian López

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 13:40:23