pandas 从熊猫数据框中的字符串列中删除 b''
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46696679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:37:17 来源:igfitidea点击:
Removing b'' from string column in a pandas dataframe
提问by John Singh
回答by cs95
You're working with byte strings. You might consider str.decode
:
您正在使用字节字符串。你可能会考虑str.decode
:
data['class'] = data['class'].str.decode('utf-8')
回答by Anton vBR
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:
基本上你对 .str() 所做的就是将它应用于所有元素。也可以这样写:
df[0].apply(lambda x: x.decode('utf-8'))