从字符串列中提取两列

huangapple go评论59阅读模式
英文:

Extract two columns from a column of strings

问题

我有一个数据框,其中包含以下格式的字符串

Rondonópolis (c/ 5,2%) 3500 7000 2789 4258

我需要创建两列并保持这种方式。我一直在尝试使用正则表达式,但仍然无法

A B
Rondonópolis (c/ 5,2%) 3500 7000 2789 4258
英文:

I have a dataframe, which contains strings in this format

Rondonópolis (c/ 5,2%) 3500 7000 2789 4258

I need to create two columns and stay that way. I've been trying to use regex but I still can't

A B
Rondonópolis (c/ 5,2%) 3500 7000 2789 4258

答案1

得分: 1

使用str.extract来提取两个组:一个是数字(四个四位数),另一个是这些数字之前的所有内容。

df = pd.DataFrame({'my_column': ["Rondonópolis (c/ 5,2%) 3500 7000 2789 4258", "Ponta Grossa 2100 3121 4578 3234"]})
df[['A', 'B']] = df['my_column'].str.extract(r"(.+) (\d{4} \d{4} \d{4} \d{4})")
英文:

Use str.extract to extract two groups: one of the numbers (four 4-digit numbers) and the other everything preceding those numbers.

df = pd.DataFrame({'my_column': ["Rondonópolis (c/ 5,2%) 3500 7000 2789 4258", "Ponta Grossa 2100 3121 4578 3234"]})
df[['A', 'B']] = df['my_column'].str.extract(r"(.+) (\d{4} \d{4} \d{4} \d{4})")

从字符串列中提取两列

huangapple
  • 本文由 发表于 2023年3月7日 06:52:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75656560.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定