what’s the best way to add a specific character between specific numbers in a string column in pandas datafrane?

If you have these strings in list use a for loop and for each use the followings:

A = A[:2] + 'A' + A[2:]

If you have them in a column of a pandas dataframe, the following will help. Assume df is your dataframe:

df = pd.DataFrame({'column1': ['16923ABCD' ,'16928ABCD' ,'16917ABCD' ,
                               '16934ABCD' , '16912ABCD']})

df['column1'] = df['column1'].astype(str).str[:2] + 'A' +
                df['column1'].astype(str).str[2:]

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top