python - 在 Pandas 列中拆分字符串和数字
问题描述
我是python的初学者。我dataframe
看起来像这样:
df
No ID
1 52Device-24-03HAntang
2 40Device-16HAntang
我想拆分ID
列。我的预期结果如下所示:
Result
No ID No_ID Device Code Name
1 52Device-24-03HAntang 52 Device-24-03 H Antang
2 40Device-16HAntang 40 Device-16 H Antang
谢谢你
解决方案
如果需要用大写字母分割,首先在大写之前添加空格Series.str.replace
,然后用这个空格分割Series.str.split
:
cols = ['No_ID','Device','Code','Name']
df[cols] = df['ID'].str.replace(r"([A-Z])", r" \1").str.split(expand=True)
print (df)
No ID No_ID Device Code Name
0 1 52Device-24-03HAntang 52 Device-24-03 H Antang
1 2 40Device-16HAntang 40 Device-16 H Antang
推荐阅读
- javascript - 使用 dynamodb SET 附加到字符串集
- react-native - 如何在 React Native 应用程序中扫描信用卡上的“16 位、到期日期、3 位”?
- flutter - 如何在颤振中修复工厂 DbHelper 类?
- for-loop - 在一个 for 循环中使用 for 循环进行批处理
- mysql - MySQL 错误代码 1452 无法添加或更新子行
- ios - 在 TestFlight 的 Apple 硅 Mac 上部署 iPhone 和 iPad 应用程序
- python - Discord.py json文件而不是添加新值,json将所有值替换为一个
- python - 尝试使用另一个脚本导入模块
- c++ - 使用 OpenSSL 解密 AES 的奇怪问题,填充相同外观的垃圾,Base64 填充问题
- python - Python For 循环单行语法