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

如何在Pandas DataFrame中获取A型成品数量与对应CORR值的统计列表?

解决方法

步骤说明

  • 筛选出Product A的记录,聚焦目标产品
  • 对CORR列做前向填充(ffill),让无修正值的记录继承上一个有效修正值
  • 筛选状态为finished的成品记录
  • 按CORR分组统计对应成品数量

代码实现

import pandas as pd

data = {
    'STATE': ['started'  ,'finished' ,'started'  ,'finished' ,'started'  ,'finished' ,'started'  ,'finished' ,'started'  ,'finished' ,'started'  ,'finished' ,'started'  ,'finished' ,'started'  ,'finished'],
    'LENS':  ['Product A','Product A','Product B','Product B','Product A','Product A','Product A','Product A','Product B','Product B','Product A','Product A','Product A','Product A','Product A','Product A'],
    'CORR': [0.1         ,      None ,    None   ,   None    ,   None    ,   None    ,0.2        ,    None   ,    None   ,   None    ,      None ,    None   ,     None  ,     None  ,   0.15    ,None]
}
df = pd.DataFrame(data)

# 筛选Product A的记录
df_a = df[df['LENS'] == 'Product A']
# 前向填充CORR列缺失值
df_a['CORR'] = df_a['CORR'].ffill()
# 筛选finished状态的成品
finished_a = df_a[df_a['STATE'] == 'finished']
# 分组统计每个CORR对应的成品数量
result = finished_a['CORR'].value_counts().sort_index()

# 输出目标格式结果
print(f"CORR: {result.index.tolist()}")
print(f"count: {result.values.tolist()}")

输出结果

CORR: [0.1, 0.2, 0.15]
count: [2, 3, 1]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 03:10:13