Series.str可用于以字符串形式访问系列的值并对其应用几种方法。 Pandas Series.str.decode()函数用于使用指示的编码来解码系列/索引中的字符串。此功能等效于str.decode()在python2和bytes.decode()在python3中。
用法: Series.str.decode(encoding, errors=’strict’)
参数:
encoding:str
errors:str,可选
返回:解码的对象序列/索引
范例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)
输出:
现在我们将使用Series.str.decode()函数解码给定系列对象的基础数据中的字符串。
# use 'UTF-8' encoding
result = sr.str.decode(encoding = 'UTF-8')
# print the result
print(result)
输出:
正如我们在输出中看到的,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)
输出:
现在我们将使用Series.str.decode()函数解码给定系列对象的基础数据中的字符串。
# use 'ASCII' encoding
result = sr.str.decode(encoding = 'ASCII')
# print the result
print(result)
输出:
正如我们在输出中看到的,Series.str.decode()函数已成功解码给定系列对象的基础数据中的字符串。