1

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.

0

2 Answers 2

2

Use np.where -

df['clients_x'] = np.where(df['clients_x'].str.startswith('['), df['clients_x'], '[' + df['clients_x'] + ']')

Using df.where -

df['clients_x'].where(df['clients_x'].str.startswith('['), '[' + df['clients_x'] + ']')

Output

0               [7110001002]
1               [7110001002]
2               [7110001002]
3    [7110052941,7110107795]
4    [7110052941,7110107795]
5    [7110052941,7110107795]
Name: clients_x, dtype: object
Sign up to request clarification or add additional context in comments.

Comments

1

You need to use where, not mask (see the doc):

df["clients_x"] = df.clients_x.where(
  df.clients_x.astype(str).str.startswith("["), 
  "[" + df.clients_x + "]"
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.