Skip to content
Advertisement

I’m trying to replace/convert all the A’s and B’s in a particular column to 1 and O in a csv file

This is the error i’m getting

TypeError                                 Traceback (most recent call last)
Input In [141], in <cell line: 2>()
      1 repl = {'Y':'1', 'N':'0'}
----> 2 prices_dataframe['col_state'] = prices_dataframe['col_state'].replace(repl, regex=True)

TypeError: 'bool' object is not subscriptable

This is what i have tried

repl = {‘Y’:’1′, ‘N’:’0′} prices_dataframe[‘col_state’] = prices_dataframe[‘col_state’].replace(repl, regex=True)

Advertisement

Answer

The replace() method is unique to strings, and at some point the value you are trying to replace is being a boolean (True or False). If you still want to replace it, you can transform the value into a string:

prices_dataframe['col_state'] = str(prices_dataframe['col_state']).replace(repl, regex=True)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement