Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

Pandas Series.str.encode() Method



TheSeries.str.encode() method in Pandas is used to encode character strings in a Series or Index into byte strings using the specified encoding. This method is useful for converting text data into encoded formats for storage or transmission.

This method is similar to thestr.encode() method, and it provides an easy way to handle encoding of text data within a Pandas Series or Index.

Syntax

Following is the syntax of the PandasSeries.str.encode() method −

Series.str.encode(encoding, errors='strict')

Parameters

TheSeries.str.encode() method accepts the following parameters −

  • encoding − A string representing the name of the encoding to use for encoding the text.

  • errors − An optional string specifying how encoding errors should be handled. The default is 'strict', which raises a UnicodeEncodeError on encoding errors. Other options include 'ignore', 'replace', 'backslashreplace', and 'namereplace'.

Return Value

TheSeries.str.encode() method returns a Series or Index of the same type as the calling object, containing the encoded byte strings.

Example

In this example, we demonstrate the basic usage of theSeries.str.encode() method by encoding a Series of strings using the 'ascii' encoding.

import pandas as pd# Create a Series of stringsser = pd.Series(['Tutorialspoint', '123', '$'])# Encode strings using 'ascii' encodingresult = ser.str.encode('ascii')print("Input Series:")print(ser)print("\nSeries after calling str.encode('ascii'):")print(result)

When we run the above code, it produces the following output −

Input Series:0    Tutorialspoint1               1232                 $dtype: objectSeries after calling str.encode('ascii'):0    b'Tutorialspoint'1               b'123'2                 b'$'dtype: object

Example

This example demonstrates how to use theSeries.str.encode() method to encode a column of strings in a DataFrame using the 'utf-8' encoding.

import pandas as pd# Create a DataFrame with a column of stringsdf = pd.DataFrame({ 'COLUMN1': ['', '', ''] })# Encode strings using 'utf-8' encodingresult = df['COLUMN1'].str.encode('utf-8')print("Input DataFrame:")print(df)print("\nDataFrame column after calling str.encode('utf-8'):")print(result)

Following is the output of the above code −

Input DataFrame:  COLUMN10       1       2     DataFrame column after calling str.encode('utf-8'):0    b'\xc2\xa9'1    b'\xe2\x82\xac'2    b'\xf0\x9f\x87\x80'Name: COLUMN1, dtype: object

Example

Here's another example demonstrating the use of theSeries.str.encode() method to encode strings with special characters using the 'utf-8' encoding.

import pandas as pd# Create a Series of strings with special charactersser = pd.Series(['', '', ''])# Encode strings using 'utf-8' encodingresult = ser.str.encode('utf-8')print("Input Series:")print(ser)print("\nSeries after calling str.encode('utf-8'):")print(result)

Following is the output of the above code −

Input Series:0    1    2    dtype: objectSeries after calling str.encode('utf-8'):0    b'\xe2\x9c\x94'1    b'\xe2\x9c\x93'2    b'\xe2\x9c\x9c'dtype: object
python_pandas_working_with_text_data.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp