0

I have the following column in pandas

Code
0.1.2 Contaminated land
1.1.1 Standard foundations (default)
1.1.2 Specialist foundations
8.1.2 Preparatory groundworks

How do I separate it into the following below?

Column A Column B
0.1.2 Contaminated land
1.1.1 Standard foundations (default)
1.1.2 Specialist foundations

enter image description here

using the following code I get the below, which does not work.

df[['code', 'description']] = df['code'].str.split(' ', 1, expand=True)
Jimmy3421
  • 41
  • 6

1 Answers1

1

Use .str.split() with 1 as the split count and expand=True to expand into series, then assign back to your df.

import pandas as pd

df = pd.DataFrame({"code": [
    '0.1.2 Contaminated land',
    '1.1.1 Standard foundations (default)',
    '1.1.2 Specialist foundations',
    '8.1.2 Preparatory groundworks',
]})

df[['code', 'description']] = df['code'].str.split(' ', 1, expand=True)
print(df)
    code                     description
0  0.1.2               Contaminated land
1  1.1.1  Standard foundations (default)
2  1.1.2          Specialist foundations
3  8.1.2         Preparatory groundworks
AKX
  • 152,115
  • 15
  • 115
  • 172
  • Thanks, strangely it worked for some columns but no others, I've add the image response in the question – Jimmy3421 Nov 14 '22 at 15:49
  • Chances are the separator is not the regular space, but eg. a nonbreaking space. You could use the `\s+` regexp to split..? – AKX Nov 14 '22 at 18:20
  • I used the following s.str.extract('(?:(.*\d))?(?:([a-zA-Z]+))?', expand=True) Thanks – Jimmy3421 Nov 15 '22 at 09:44