Python | Pandas Series.str.encode ()
Series.str can be used to access the values of the series as strings and apply several methods to it. Pandas Series.str.encode() function is used to encode character string in the Series/Index using indicated encoding. Equivalent to str.encode().
Syntax: Series.str.encode(encoding, errors=’strict’)
Parameter :
encoding : str
errors : str, optionalReturns : encoded : Series/Index of objects
Example #1: Use Series.str.encode() function to encode the character strings present in the underlying data of the given series object. Use ‘raw_unicode_escape’ for encoding.
Выход :

Now we will use Series.str.encode() function to encode the character strings present in the underlying data of the given series object.
# use "raw_unicode_escape" encodingresult = sr.str.encode(encoding = "raw_unicode_escape") # print the resultprint(result) |
Выход :

As we can see in the output, the Series.str.encode() function has successfully encoded the strings in the given series object.
Example #2 : Use Series.str.encode() function to encode the character strings present in the underlying data of the given series object. Use ‘punycode’ for encoding.
# importing pandas as pdimport pandas as pd # Creating the Seriessr = pd.Series(["Mike", "Alessa", "Nick", "Kim", "Britney"]) # Creating the indexidx = ["Name 1", "Name 2", "Name 3", "Name 4", "Name 5"] # set the indexsr.index = idx # Print the seriesprint(sr) |
Выход :

Now we will use Series.str.encode() function to encode the character strings present in the underlying data of the given series object.
# use "punycode" encodingresult = sr.str.encode(encoding = "punycode") # print the resultprint(result) |
Выход :

As we can see in the output, the Series.str.encode() function has successfully encoded the strings in the given series object.
Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.
Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.