Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

Pandas Series.str.get() Method



TheSeries.str.get() method in Python Pandas is used for extracting elements from various data structures contained within each element of a Series or Index. Whether you're working with lists, tuples, dictionaries, or strings, this method allows you to specify the position or key of the element you want to extract.

It simplifies the process of accessing nested data and is highly useful in data cleaning and preprocessing tasks.

Syntax

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

Series.str.get(i)

Parameters

TheSeries.str.get() method accepts the following parameter −

  • i − An integer or hashable dictionary label representing the position or key of the element to extract.

Return Value

TheSeries.str.get() method returns a Series or Index with the extracted elements.

Example 1

This example demonstrates extracting elements from lists within a DataFrame column using theSeries.str.get() method.

import pandas as pd# Create a DataFrame with a column of listsdf = pd.DataFrame({"A": [[1, 2, 3], [0, 1, 3]], "B": ['Tutorial', 'AEIOU']})print("Original DataFrame:")print(df)# Extract the element at index 1 from each list in column 'A'df['C'] = df['A'].str.get(1)print("\nDataFrame after extracting elements from lists in column 'A':")print(df)

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

Original DataFrame:           A         B0  [1, 2, 3]  Tutorial1  [0, 1, 3]     AEIOUDataFrame after extracting elements from lists in column 'A':           A         B  C0  [1, 2, 3]  Tutorial  21  [0, 1, 3]     AEIOU  1

The new column 'C' contains the elements extracted from index 1 of each list in column 'A'.

Example 2

This example demonstrates extracting characters from strings within a DataFrame column using theSeries.str.get() method.

import pandas as pd# Create a DataFrame with a column of stringsdf = pd.DataFrame({"A": [[1, 2, 3], [0, 1, 3]], "B": ['Tutorial', 'AEIOU']})print("Original DataFrame:")print(df)# Extract the character at index 1 from each string in column 'B'df['D'] = df['B'].str.get(1)print("\nDataFrame after extracting characters from strings in column 'B':")print(df)

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

Original DataFrame:           A         B0  [1, 2, 3]  Tutorial1  [0, 1, 3]     AEIOUDataFrame after extracting characters from strings in column 'B':           A         B  D0  [1, 2, 3]  Tutorial  u1  [0, 1, 3]     AEIOU  E

Example 3

This example demonstrates extracting values from dictionaries within a DataFrame column using theSeries.str.get() method.

import pandas as pd# Create a DataFrame with a column of dictionariesdf = pd.DataFrame({"A": [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}], "B": ['Dict1', 'Dict2']})print("Original DataFrame:")print(df)# Extract the value associated with key 'a' from each dictionary in column 'A'df['C'] = df['A'].str.get('a')print("\nDataFrame after extracting values from dictionaries in column 'A':")print(df)

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

Original DataFrame:             A      B0  {'a': 1, 'b': 2}  Dict11  {'a': 3, 'b': 4}  Dict2DataFrame after extracting values from dictionaries in column 'A':             A      B  C0  {'a': 1, 'b': 2}  Dict1  11  {'a': 3, 'b': 4}  Dict2  3
python_pandas_working_with_text_data.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp