
- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
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