8

I have a data frame as taken from SDSS database. Example data is here.

img

I want to remove the character 'b' from data['class']. I tried

data['class'] = data['class'].replace("b','')

But I am not getting the result.

CDspace
  • 2,611
  • 17
  • 32
  • 36
John Singh
  • 67
  • 1
  • 1
  • 8

2 Answers2

27

You're working with byte strings. You might consider str.decode:

data['class'] = data['class'].str.decode('utf-8') 
cs95
  • 330,695
  • 80
  • 606
  • 657
1

Further explanation:

df = pd.DataFrame([b'123']) # create dataframe with b'' element

Now we can call

df[0].str.decode('utf-8') # returns a pd.series applying decode on str succesfully
df[0].decode('utf-8') # tries to decode the series and throws an error

Basically what you are doing with .str() is applying it for all elements. It could also be written like this:

df[0].apply(lambda x: x.decode('utf-8')) 
Anton vBR
  • 16,833
  • 3
  • 36
  • 44