Multiple special character transformations on dataframe using Pandas
Question:
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
Answers:
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
We can try using str.extract
here:
df["ID"] = df["ID"].str.extract(r'(w+)')
df["Type"] = df["Type"].str.extract(r'(w+)')
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
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
We can try using str.extract
here:
df["ID"] = df["ID"].str.extract(r'(w+)')
df["Type"] = df["Type"].str.extract(r'(w+)')