Python Pandas Series.str.decode()

Python Pandas Series.str.decode()

Series.str可以用来访问作为字符串的系列值,并对其应用几种方法。Pandas Series.str.decode()函数用于对Series/Index中的字符串进行解码,使用指定的编码。这个函数等同于python2中的str.decode()和python3中的bytes.decode()。

语法:

Series.str.decode(encoding, errors=strict)
Python

参数:
encoding: str
errors: str, optional

返回:解码的系列/对象索引

示例#1:使用Series.str.decode()函数对给定系列对象的底层数据中的字符串进行解码。使用’UTF-8’编码方法。

# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series([b"b'New_York'", b"b'Lisbon'", b"b'Tokyo'", b"b'Paris'", b"b'Munich'"])
  
# Creating the index
idx = ['City 1', 'City 2', 'City 3', 'City 4', 'City 5']
  
# set the index
sr.index = idx
  
# Print the series
print(sr)
Python

输出 :

Python Pandas Series.str.decode()

现在我们将使用Series.str.decode()函数来解码给定系列对象的底层数据中的字符串。

# use 'UTF-8' encoding
result = sr.str.decode(encoding = 'UTF-8')
  
# print the result
print(result)
Python

输出 :

Python Pandas Series.str.decode()

正如我们在输出中看到的,Series.str.decode()函数已经成功解码了给定系列对象的底层数据中的字符串。

示例#2 :使用Series.str.decode()函数对给定系列对象的底层数据中的字符串进行解码。使用’ASCII’编码方法。

# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series([b'Mike-', b'Alessa-', b'Nick-', b'Kim-', b'Britney-'])
  
# Creating the index
idx = ['Name 1', 'Name 2', 'Name 3', 'Name 4', 'Name 5']
  
# set the index
sr.index = idx
  
# Print the series
print(sr)
Python

输出 :

Python Pandas Series.str.decode()

现在我们将使用Series.str.decode()函数来解码给定系列对象的底层数据中的字符串。

# use 'ASCII' encoding
result = sr.str.decode(encoding = 'ASCII')
  
# print the result
print(result)
Python

输出 :

Python Pandas Series.str.decode()

正如我们在输出中看到的,Series.str.decode()函数已经成功解码了给定系列对象的底层数据中的字符串。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册