简体   繁体   English

从熊猫数据框中的字符串列中删除b''

[英]Removing b'' from string column in a pandas dataframe

I have a data frame as taken from SDSS database. 我有一个从SDSS数据库获取的数据框。 Example data is here. 示例数据在这里。

img

I want to remove the character 'b' from data['class'] . 我想从data['class']删除字符'b'。 I tried 我试过了

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

But I am not getting the result. 但是我没有得到结果。

You're working with byte strings. 您正在使用字节字符串。 You might consider str.decode : 您可能会考虑str.decode

data['class'] = data['class'].str.decode('utf-8') 

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. 基本上,您使用.str()所做的就是将其应用于所有元素。 It could also be written like this: 也可以这样写:

df[0].apply(lambda x: x.decode('utf-8')) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM