I have a dataframe that looks like this:
clients_x clients_y coords_x coords_y
7110001002 7100019838 -23.63013,-46.704887 -23.657433,-46.744095
7110001002 7100021875 -23.63013,-46.704887 -23.7729,-46.591366
7110001002 0700245857 -23.63013,-46.704887 -23.7074,-46.5698
[7110052941, 7110107795] 7100019838 -23.609,-46.6974 -23.657433,-46.744095
[7110052941, 7110107795] 7100021875 -23.609,-46.6974 -23.7729,-46.591366
[7110052941, 7110107795] 0700245857 -23.609,-46.6974 -23.7074,-46.569
What I want to do is for all values in clients_x column to start and end with "[ ]". Therefore, my expected output is this one:
clients_x clients_y coords_x coords_y
[7110001002] 7100019838 -23.63013,-46.704887 -23.657433,-46.744095
[7110001002] 7100021875 -23.63013,-46.704887 -23.7729,-46.591366
[7110001002] 0700245857 -23.63013,-46.704887 -23.7074,-46.5698
[7110052941, 7110107795] 7100019838 -23.609,-46.6974 -23.657433,-46.744095
[7110052941, 7110107795] 7100021875 -23.609,-46.6974 -23.7729,-46.591366
[7110052941, 7110107795] 0700245857 -23.609,-46.6974 -23.7074,-46.569
To do that first I tried to do something like this:
df["clients_x"] = "[" + "df["clients_x"]" + "]"
However, by doing that will actually add "[ ]" at the beginning and end of each value, but for those rows that already have "[ ]" will duplicate them. The output is this one:
clients_x clients_y coords_x coords_y
[7110001002] 7100019838 -23.63013,-46.704887 -23.657433,-46.744095
[7110001002] 7100021875 -23.63013,-46.704887 -23.7729,-46.591366
[7110001002] 0700245857 -23.63013,-46.704887 -23.7074,-46.5698
[[7110052941, 7110107795]] 7100019838 -23.609,-46.6974 -23.657433,-46.744095
[[7110052941, 7110107795]] 7100021875 -23.609,-46.6974 -23.7729,-46.591366
[[7110052941, 7110107795]] 0700245857 -23.609,-46.6974 -23.7074,-46.569
To avoid that issue I've tried using the following code where basically I want to add "[ ]" at the beginning and at the end of each value in the clients_x column that starts with a digit.
df['clients_x'] = df['clients_x'].mask(df['clients_x'].astype(str).str.startswith(r'^\d'), f'[{df.clients_x}]')
However, the output that this line of code is generating is the same as my original dataframe. If anyone has any idea about how to approach this problem I would really appreciate your help.