Search code examples
pythonpandasdata-cleaningdata-wrangling

How to add every 2 characters in a pandas series df


I have a pandas df that looks like this:

enter image description here

I'm trying to split up the Gun_Time and Net_Time columns by adding a ':' after every 2 numbers.
I've tried some regex with a simple function but have been unable to come up with the correct solution to correctly insert a ':' after every 2 numbers. How could I achieve this?


Solution

  • You can use a lambda function and apply. Are you trying to convert these values into timestamps ?

    df.Gun_Time.astype(str).apply(lambda s: s[:2] + ':' + s[2:])