python - 熊猫中的字符串到浮点转换错误[无法将字符串转换为浮点:'dvic']
问题描述
我正在尝试从标题列中提取年份并转换为“int”或“float”。转换时显示错误(标记为黄色)
链接-> https://colab.research.google.com/drive/1nGCdvCLUQYqU8zsEF3PaGrng-ay3IkIu?usp=sharing
解决方案
考虑title
列仅由年份形式的数字组成。
如果要将名为的新列year
添加到 df
df['year'] = df.title.str.extract(r"\((\d+)\)")
如果你想year
作为一个列表/系列
year = df.title.str.extract(r'(\d+)')
输出 在样本的最后一行添加了一个虚拟标题
movieId title genres year
0 1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy 1995
1 2 Jumanji (1995) Adventure|Children|Fantasy 1995
2 3 Grumpier Old Men (1995) Comedy|Romance 1995
3 4 Waiting to Exhale (1995) Comedy|Drama|Romance 1995
4 5 Father of the Bride Part II (1995) Comedy 1995
5 6 Dummy 3 Title (1995) Comedy 1995
推荐阅读
- google-chrome-extension - 使用 Chrome 扩展 chrome.storage.local 的无限存储空间?
- android - Main Activity 加载时无法启动
- reactjs - 将 React 与 Web 组件一起使用时缺少样式
- javascript - 我们如何将新的输入值放入另一个元素中?
- build - 如何在 Azure Dev Ops 中查看我的构建项目文件夹的内容?
- xcode - 如何让 SKSpriteNode 来回遵循相同的路径?
- django - 尝试在 Django 中关注用户时出错
- cdi - 如果所有事件都有 @Any 作为限定符,那么观察没有限定符的事件意味着什么?
- javascript - 用作 JSX - React Native
- linux-kernel - 是否可以在不编译 Linux 内核本身的情况下编译自定义 Linux 内核驱动程序?