0

I have these items on a dataframe column and would want to split them into various components and have each component on a different column. For instance, I want to split c5-0c0s9a0l14 so that i will have c5 | 0|c0|s9|a0|l14

I'm not sure how to do this but will appreciate any assistance with it

I tried doing this but it's not working

df[['item', 'val']] = df['COMPONENT_NAME'].str.split('-', expand=True)

see input and expected output format below: enter image description here

maks
  • 11
  • 4
  • 2
    post a testable input df fragment and expected output – RomanPerekhrest Jul 08 '23 at 15:29
  • Are you using Pandas? Please [edit] and add the tag for it. – wjandrea Jul 08 '23 at 16:04
  • What's the rule? Split on every dash or after every number? You could do that in regex. – wjandrea Jul 08 '23 at 16:06
  • Try looking at [How to split a dataframe string column into two columns?](https://stackoverflow.com/q/14745022/16653700) – Alias Cartellano Jul 08 '23 at 16:27
  • I have edited my question to include the input and expected output format. I appreciate any help I could get. Thanks – maks Jul 08 '23 at 22:15
  • [Please don't post pictures of text](https://meta.stackoverflow.com/q/285551/4518341). Instead, copy the text itself, [edit] it into your post, and use the formatting tools like [code formatting](/editing-help#code). – wjandrea Jul 09 '23 at 15:30

1 Answers1

-1

Here, I have written the snippet for separating columns hopefully this will help you further. Also, attached the output so you have a clear understanding of it.

import pandas as pd

df = pd.DataFrame({'Input': ['c5-0c0s9a0l14', 'c4-0c0s9a0l14', 'c3-0c0s9a0l14']})
df = df['Input'].str.split('-', expand=True)
print(df)
    0           1
0  c5  0c0s9a0l14
1  c4  0c0s9a0l14
2  c3  0c0s9a0l14
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Nimra Tahir
  • 391
  • 1
  • 6