python - 将字符串转换为数据框中列内的整数(5 星级 = 5)
问题描述
我想将包含评论字符串的列转换为整数,例如 5.0 颗星(满分 5 颗星)。
0 5.0 out of 5 stars
1 2.0 out of 5 stars
2 5.0 out of 5 stars
3 5.0 out of 5 stars
4 5.0 out of 5 stars
5 5.0 out of 5 stars
6 4.0 out of 5 stars
7 5.0 out of 5 stars
8 5.0 out of 5 stars
9 5.0 out of 5 stars
Name: StarRating, dtype: object
我熟悉遍历行和列,并尝试过
df[["StarRating"]] = df[["StarRating"]] .apply(pd.to_numeric)
但收到以下错误
ValueError: Unable to parse string "5.0 out of 5 stars" at position 0
我也试过:
for col in df.StarRating()
if df['StarRating'] = (df['StarRating'] !='5.0 out of 5 stars').astype(int, 5.0)
if df['StarRating'] = (df['StarRating'] !='4.0 out of 4 stars').astype(int, 4.0)
if df['StarRating'] = (df['StarRating'] !='3.0 out of 3 stars').astype(int, 3.0)
if df['StarRating'] = (df['StarRating'] !='2.0 out of 2 stars').astype(int, 2.0)
if df['StarRating'] = (df['StarRating'] !='1.0 out of 1 stars').astype(int, 1.0)
print(StarInt)
但收到错误
File "<ipython-input-43-e2e6fd3fae34>", line 1
for col in df.StarRating()
^
SyntaxError: invalid syntax
任何建议将不胜感激。谢谢
解决方案
尝试拆分字符串并将第一个元素转换为浮点数:
df['StarRatingNumeric'] = df.StarRating.apply(lambda r: float(r.split()[0]))
或者如果您需要整数数据类型:
df['StarRatingNumeric'] = df.StarRating.apply(lambda r: int(float(r.split()[0])))
推荐阅读
- jenkins - 如何从 Jenkinsfile 步骤中删除脚本中的脚本
- json - 转换响应实体
到列表 [科特林] - flutter - 任何人都知道我是否可以通过 Flutter Web 以某种方式提供 json 文件?
- javascript - 无法读取 this.sidenav.toggle() 中未定义的属性“切换”;
- data-structures - 如何存储链接切割树?
- c# - 性能不佳的后台服务从队列中出列
- zephyr-rtos - 如何在 zephyr-rtos 项目中添加应用程序单元测试
- css - 停止 flexbox 项目 "flex:1" 占用由子宽度引起的更多空间
- javascript - VueJS + Firebase 身份验证在脚本加载之前无法访问会话
- bootbox - 如何在引导箱提示中仅显示确认按钮?