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

Pandas分组后如何提取分组列的指定单个值

提取分组列的单个值
  • 方法1:直接提取索引值
    分组后STRSTATE会成为DataFrame的索引,直接取索引的第一个元素即可:

    target_value = Gb.index[0]
    print(target_value)  # 输出 'OR'
    
  • 方法2:重置索引后提取列值
    先将索引转换为普通列,再通过列名+位置提取:

    Gb_reset = Gb.reset_index()
    target_value = Gb_reset['STRSTATE'].iloc[0]
    print(target_value)  # 输出 'OR'
    
  • 方法3:通过索引值数组提取
    利用index.values获取索引的数组形式,再取第一个元素:

    target_value = Gb.index.values[0]
    print(target_value)  # 输出 'OR'
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 21:15:36