Skip to content Skip to sidebar Skip to footer

Pandas Add Space Between Characters If Needed

Given the following data frame: import pandas as pd import numpy as np df = pd.DataFrame({'A':['One[P]','Two [N]'], }) df A 0 One[P] 1 Two [N] I'd like to add a space bef

Solution 1:

Or you can use:

df.loc[~df.A.str.contains(r' \['), 'A'] = df.A.str.replace('[',' [')
print df
         A
0  One [P]
1  Two [N]

Solution 2:

alternatively, you can use RegEx's:

In [138]: df.A = df.A.replace(r'([^\s])\[', r'\1 [', regex=True)

In [139]: df
Out[139]:
         A
0  One [P]
1  Two [N]

Post a Comment for "Pandas Add Space Between Characters If Needed"