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

如何对Pandas Series中带/的字符串拆分后计算平均值?

问题描述

我有一个Pandas Series,其唯一值如下:

array(['17', '19', '21', '20', '22', '23', '12', '13', '15', '24', '25',
       '18', '16', '14', '26', '11', '10', '12/16', '27', '10/14',
       '16/22', '16/21', '13/17', '14/19', '11/15', '10/15', '15/21',
       '13/19', '13/18', '32', '28', '12/15', '29', '42', '30', '31',
       '34', '46', '11/14', '18/25', '19/26', '17/24', '19/24', '17/23',
       '13/16', '11/16', '15/20', '36', '17/25', '19/25', '17/22',
       '18/26', '39', '41', '35', '50', '9/13', '33', '10/13', '9/12',
       '93/37', '14/20', '10/16', '14/18', '16/23', '37', '9/11', '37/94',
       '20/54', '22/31', '22/30', '23/33', '44', '40', '50/95', '38',
       '16/24', '15/23', '15/22', '18/23', '16/20', '37/98', '19/27',
       '38/88', '23/31', '14/22', '45', '39/117', '28/76', '33/82',
       '15/19', '23/30', '47', '46/115', '14/21', '17/18', '25/50',
       '12/18', '12/17', '21/28', '20/27', '26/58', '22/67', '22/47',
       '25/51', '35/83', '39/86', '31/72', '24/56', '30/80', '32/85',
       '42/106', '40/99', '30/51', '21/43', '52', '56', '25/53', '34/83',
       '30/71', '27/64', '35/111', '26/62', '32/84', '39/95', '18/24',
       '22/29', '42/97', '48', '55', '58', '39/99', '49', '43', '40/103',
       '22/46', '54/133', '25/54', '36/83', '29/72', '28/67', '35/109',
       '25/62', '14/17', '42/110', '52/119', '20/60', '46/105', '25/56',
       '27/65', '25/74', '21/49', '29/71', '26/59', '27/62'], dtype=object)

其中带有/的字符串,需要拆分成数组后计算两个值的平均值(例如'18/25'转为[18,25]后取整为22)。

尝试过提取第一个值的方法:

master_data["Cmb MPG"].str.split('/').str[0].astype('int8')

但不符合需求。还尝试了以下代码:

np.array(master_data["Cmb MPG"].str.split('/')).astype('int8').mean()

出现报错:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
Cell In[88], line 1
----> 1 np.array(master_data["Cmb MPG"].str.split('/')).astype('int8')

ValueError: setting an array element with a sequence.

使用slice()方法也无法完成字符串拆分,请问如何正确实现需求?

解决方案

可以通过以下几种简洁方式实现需求:

方法1:str.split结合apply计算均值

利用str.split拆分字符串后,用apply遍历每个结果,判断长度后计算均值或直接转整数:

master_data["Cmb MPG"].str.split('/').apply(
    lambda x: round((int(x[0]) + int(x[1]))/2) if len(x) == 2 else int(x[0])
)

方法2:正则提取数字后计算均值

通过正则匹配提取所有数字,转为数值型后计算行均值,自动处理单值行:

import pandas as pd

# 提取数字到两列,转为浮点型
mpg_numbers = master_data["Cmb MPG"].str.extract(r'(\d+)/?(\d*)', expand=True).astype(float)
# 计算均值,空值用第一列填充,最后取整转整数
mpg_avg = mpg_numbers.mean(axis=1).fillna(mpg_numbers[0]).round().astype(int)

方法3:拆分后转为DataFrame计算均值

用expand=True将拆分结果转为DataFrame,转为数值型后直接计算行均值:

split_df = master_data["Cmb MPG"].str.split('/', expand=True).apply(pd.to_numeric, errors='coerce')
mpg_avg = split_df.mean(axis=1).round().astype(int)
  • 逻辑:单值行的第二列为NaN,均值自动等于第一列值,无需额外判断。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 08:14:52