0

I got a dataframe that contains columns with numbers where some are a single number (1234) and others contains multiple numbers(12340-567-8900) I found a way to split the multiple numbers, but if my columns contains single numbers my code fails.

Can anyone point me in the right direction?

    `lens = df['EPDNr'].str.split('-').map(len)
     res = pd.DataFrame({'Title':np.repeat(df['Title'],lens),
                'Kundenummer':np.repeat(df['Kundenummer'],lens),
                'Avvikstype':np.repeat(df['Avvikstype'],lens),
                'Ordrenummer':np.repeat(df['Ordrenummer'],lens),
                'EPDNr':chainer(df['EPDNr']),
               'Antall':chainer(df['Antall'])})

I want my output to look like Column header: EPDNR Cell a1 1234 Cell a2 12340 Cell a3 567 cell a4 8900 ect..

2
  • 2
    Can you provide more complete sample dataset and expected outputs from that data set? Commented Jun 11, 2019 at 18:13
  • pd.Series(['1234-567-8900','1234']).str.split('-').str[0] Commented Jun 11, 2019 at 18:23

1 Answer 1

2

You can use expand=True:

In [11]: s = pd.Series(["1234-567-8900", "12"])

In [12]: s.str.split("-", expand=True)
Out[12]:
      0     1     2
0  1234   567  8900
1    12  None  None
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.