python - Str 仅在特定位置拆分
问题描述
嗨,我怎么能在一个数字后分割一个点并只为原始 df 生成两列?
df.name
``
0 1. The start
1 2. Today's world
2 3. Today's world vs. yesterday.
...
20 20. The change
预期输出:
number title
0 1 The start
1 2 Today's world
2 3 Today's world vs. yesterday.
...
20 20 The change
我试过了
df[['number', 'title']] = df.name.str.split('(\d+.)', expand=True)
解决方案
用于Series.str.extract
除以之前的整数.
和其他值:
df[['number', 'title']] = df.name.str.extract('(^\d+)\.\s+(.*)')
print (df)
name number title
0 1. The start 1 The start
1 2. Today's world 2 Today's world
2 3. Today's world vs. yesterday. 3 Today's world vs. yesterday.
20 20. The change 20 The change
推荐阅读
- azure - Azure 上的个性化报价解决方案模板
- swift3 - 在swift3中使用alamofire从url字符串中删除反斜杠
- c# - 如何使用 NetworkStream 分别读取数据?
- powershell - 将文件名添加到数组?
- python - 如何在python中使用正则表达式提取特定单词之前的单词
- docker - Docker 容器不使用主机 DNS
- java - AES 算法在我的数据字符串中间返回垃圾字符
- android - Volley -JsonArrayRequest 显示错误
- digital-ocean - 次要阅读偏好
- ldap - 将 WSO2 IS 配置为外部 LDAP