[英]Removing b'' from string column in a pandas dataframe
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.