[SOLVED] Remove Square brackets for one of the column based on filter condition using pandas dataframe

Issue

I have dataframe which consists two columns text column and server column, In text column i have to remove square brackets based on server column condition i.e.,[where server column == DEV].

                                     Input Dataframe
        Text Column                                   Server Column
 ['java error:404 URL not found']                        DEV
 ['java error:500 internal server']                      DEV
 HTTP status 204 (No Content)                            PROD
 HTTP status 200 created successfully                    PROD



                                   Output Dataframe
  Text Column                                         Server Column
 'java error:404 URL not found'                          DEV
 'java error:500 internal server'                        DEV
 HTTP status 204 (No Content)                            PROD
 HTTP status 200 created successfully                    PROD


####################################################
I tried with below code but not working 
data =data[data['server']== 'DEV'] 
data =data["nodes"].apply(lambda x: ",".join(x) if isinstance(x, list) else x) print(data)
But above code snippet is not working

Solution

Assuming that df is your DataFrame, this task can be done as follow.

dev_text = df[df['Server Column'] == 'DEV']['Text Column']
dev_text = dev_text.str.replace('[', repl = '', regex = False).str.replace(']', repl  = '', regex = False)
df.loc[df['Server Column'] == 'DEV', 'Text Column'] = dev_text.values

Answered By – Arty

Answer Checked By – Candace Johnson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *