Pandas Series.str.get() Method



The Series.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 Pandas Series.str.get() method −

Series.str.get(i)

Parameters

The Series.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

The Series.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 the Series.str.get() method.

import pandas as pd

# Create a DataFrame with a column of lists
df = 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         B
0  [1, 2, 3]  Tutorial
1  [0, 1, 3]     AEIOU

DataFrame after extracting elements from lists in column 'A':
           A         B  C
0  [1, 2, 3]  Tutorial  2
1  [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 the Series.str.get() method.

import pandas as pd

# Create a DataFrame with a column of strings
df = 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         B
0  [1, 2, 3]  Tutorial
1  [0, 1, 3]     AEIOU

DataFrame after extracting characters from strings in column 'B':
           A         B  D
0  [1, 2, 3]  Tutorial  u
1  [0, 1, 3]     AEIOU  E

Example 3

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

import pandas as pd

# Create a DataFrame with a column of dictionaries
df = 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      B
0  {'a': 1, 'b': 2}  Dict1
1  {'a': 3, 'b': 4}  Dict2

DataFrame after extracting values from dictionaries in column 'A':
             A      B  C
0  {'a': 1, 'b': 2}  Dict1  1
1  {'a': 3, 'b': 4}  Dict2  3
python_pandas_working_with_text_data.htm
Advertisements