-1

I wish to keep everything before the hyphen in one column, and keep everything before the colon in another column using Pandas.

Data

ID            Type               Stat

AA - type2    AAB:AB33:77:000    Y
CC - type3    CCC:AB33:77:000    N

Desired

ID    Type

AA    AAB
CC    CCC

Doing

separator = '-'
result_1 = my_str.split(separator, 1)[0]

Any suggestion is appreciated

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Lynn
  • 4,292
  • 5
  • 21
  • 44

2 Answers2

1

I would say

func1 = lambda _: _['ID'].split('- ')[0]
func2 = lambda _: _['Type'].split(':')[0]

data\
  .assign(ID=func1)\
  .assign(Type=func2)

References https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.assign.html

rafidini
  • 216
  • 1
  • 8
1

We can try using str.extract here:

df["ID"] = df["ID"].str.extract(r'(\w+)')
df["Type"] = df["Type"].str.extract(r'(\w+)')
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360