0

I have a dataframe column and i need to split a column with "," and even if no "," present in the value.

Value
=====
59.5

59.5, 5

60

60,5

desired output

value1  value2

======  ======

59.5

59.5      5

60        

60        5

Tried the code but getting an below error:

df['value1'], df_merge['value2'] = df['value'].str.split(',', 1).str

ValueError: not enough values to unpack (expected 2, got 1)

ALollz
  • 54,844
  • 7
  • 56
  • 77
  • 1
    Does this answer your question? [Pandas split column into multiple columns by comma](https://stackoverflow.com/questions/37600711/pandas-split-column-into-multiple-columns-by-comma) – Henry Ecker Apr 07 '21 at 19:50

2 Answers2

0

You could search for "," first and only do the split if str contains ",".

cwittah
  • 329
  • 1
  • 2
  • 15
0

Use the string method str.partition. It returns a 3 tuple every time with what's on the left of your chosen partition (you would choose a comma) and whats to the right of it.

df['value1'], _, df_merge['value2'] = str(df['value']).partition(',')