Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python string conversion, take out spaces, add hyphens

I have a column in a pandas data frame that is formatted like

f1 d3 a4 0a d0 6a 4b 4a 83 d4 4f c9 1f 15 11 17

and I want to convert it to look like:

f1d3a40a-d06a-4b4a-83d4-4fc91f151117

I know I can use replace(" ", "") to take the whitespace out, but I am not sure how to insert the hyphens in the exact spots that I need them.

I am also not sure how to apply it to a pandas series object.

Any help would be appreciated!

like image 931
Joe S Avatar asked Jul 01 '26 14:07

Joe S


1 Answers

This looks like a UUID, so I'd just use that module

>>> import uuid
>>> s = 'f1 d3 a4 0a d0 6a 4b 4a 83 d4 4f c9 1f 15 11 17'
>>> uuid.UUID(''.join(s.split()))
UUID('f1d3a40a-d06a-4b4a-83d4-4fc91f151117')
>>> str(uuid.UUID(''.join(s.split())))
'f1d3a40a-d06a-4b4a-83d4-4fc91f151117'

EDIT:

df = pd.DataFrame({'col':['f1 d3 a4 0a d0 6a 4b 4a 83 d4 4f c9 1f 15 11 17',
                          'f1 d3 a4 0a d0 6a 4b 4a 83 d4 4f c9 1f 15 11 17']})

df['col'] = df['col'].str.split().str.join('').apply(uuid.UUID)
print (df)
                                    col
0  f1d3a40a-d06a-4b4a-83d4-4fc91f151117
1  f1d3a40a-d06a-4b4a-83d4-4fc91f151117
like image 155
Cory Kramer Avatar answered Jul 03 '26 03:07

Cory Kramer