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
  • 57,915
  • 7
  • 66
  • 89

2 Answers2

0

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

cwittah
  • 349
  • 1
  • 3
  • 17
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(',')