如何将正则表达式应用于熊猫的拆分列?

时间:2020-07-31 01:47:30

标签: python python-3.x regex pandas

我有一列看起来像这样的字符串:

Accommodation and food services [72]
Wholesale trade [41]
Trade [41-45N]

括号内的字符串没有模式,我想将其分为两列,一列带有行业名称,另一列带有行业ID(括号内的信息)。我尝试了很多方法,但是没有任何效果。预先感谢。

[output] columnA:                                  columnB:
         Accommodation and food services           [72]
         Wholesale trade                           [41]
         Trade                                     [41-45N]

2 个答案:

答案 0 :(得分:0)

让我们做rsplit

newdf=df['col'].str.rsplit(' ', n=1, expand=True)

答案 1 :(得分:0)

您可以尝试:

pat = '^(.*) (\[.*\])$'
df['strCol'].str.extract(pat)
相关问题