-1

How to use replace method on multiple columns col0,col1,col2

new_df['col0'].str.replace(']]', ']')

1 Answers1

1

Use DataFrame.replace:

new_df = pd.DataFrame({'col0':[']]','aa]]'],
                       'col1':[']]','[s]'],
                       'col2':['[[]]]]',']'],
                       'col3':[']]', '[[]]]']})
print (new_df)
   col0 col1    col2   col3
0    ]]   ]]  [[]]]]     ]]
1  aa]]  [s]       ]  [[]]]

cols = ['col0','col1','col2']
new_df[cols] = new_df[cols].replace(']]', ']')
print (new_df)
   col0 col1    col2   col3
0     ]    ]  [[]]]]     ]]
1  aa]]  [s]       ]  [[]]]

If want replace substrings add regex=True with escaping by \ because [ is special regex character:

cols = ['col0','col1','col2']
new_df[cols] = new_df[cols].replace('\]\]', ']', regex=True)
print (new_df)
  col0 col1  col2   col3
0    ]    ]  [[]]     ]]
1  aa]  [s]     ]  [[]]]

EDIT: If possible, convert values to lists:

import ast

test=pd.DataFrame({'gender':['M']*4,'B':[[['Office/Work'],['31-35'], ['Salaried']],[['Movies,Restaurants'],['21-25'], ['Salaried']],[[ 'College/Park'],['21-25'],['Student']],[['College'], ['21-25'], ['Student']]]})
test = test.astype(str)
print (test)
  gender                                                  B
0      M         [['Office/Work'], ['31-35'], ['Salaried']]
1      M  [['Movies,Restaurants'], ['21-25'], ['Salaried']]
2      M         [['College/Park'], ['21-25'], ['Student']]
3      M              [['College'], ['21-25'], ['Student']]

df = pd.DataFrame([[y[0] for y in ast.literal_eval(x)] for x in test['B']],
                   columns=['a','b','c'])
print (df)
                    a      b         c
0         Office/Work  31-35  Salaried
1  Movies,Restaurants  21-25  Salaried
2        College/Park  21-25   Student
3             College  21-25   Student

Solution with replace:

df = test['B'].replace(['\[\[','\]\]', '\], \['], ['', '', ';'], regex=True).str.split(';', expand=True)
print (df)
                      0        1           2
0         'Office/Work'  '31-35'  'Salaried'
1  'Movies,Restaurants'  '21-25'  'Salaried'
2        'College/Park'  '21-25'   'Student'
3             'College'  '21-25'   'Student'
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • if i want to replace `,[` what need to be done also please how to remove `['` –  Jan 02 '20 at 13:54
  • @ain - Can you create some sample data, [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) ? – jezrael Jan 02 '20 at 13:56
  • test=pd.DataFrame({'gender':['M'],'B':[[['Office/Work'],['31-35'], ['Salaried']]}) > out > 'Office/Work', '31-35', 'Salaried' –  Jan 02 '20 at 14:01