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

如何在Python中将分数形式的对象列转换为整数?

解决评分列格式转换问题

报错原因

你遇到的ValueError: invalid literal for int() with base 10: '100/100',是因为'100/100'是带分隔符的字符串,既不是纯整数也不是纯浮点数格式,直接用astype(int)或int(float())无法解析。

可行解决方案

假设你的数据集是pandas的DataFrame,评分列名为score,以下两种方法都能快速转换:

方法1:拆分字符串提取前缀

通过str.split按/拆分字符串,取第一个元素后转整数:

import pandas as pd
df['score'] = df['score'].str.split('/').str[0].astype(int)

方法2:正则提取数字

用正则匹配字符串中的第一个连续数字,提取后转整数:

df['score'] = df['score'].str.extract(r'(\d+)').astype(int)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 06:55:22