Text Data Handling in Pandas

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This lab introduces how to handle text data in pandas. We will learn how to store text data, use string methods for data preprocessing and transformation, and extract substrings using regular expressions. We will also learn how to create dummy variables for machine learning algorithms.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) pandas(("`Pandas`")) -.-> pandas/DataSelectionGroup(["`Data Selection`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") pandas/DataSelectionGroup -.-> pandas/select_columns("`Select Columns`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/strings("`Strings`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("`Data Analysis`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-65455{{"`Text Data Handling in Pandas`"}} python/with_statement -.-> lab-65455{{"`Text Data Handling in Pandas`"}} pandas/select_columns -.-> lab-65455{{"`Text Data Handling in Pandas`"}} python/variables_data_types -.-> lab-65455{{"`Text Data Handling in Pandas`"}} python/strings -.-> lab-65455{{"`Text Data Handling in Pandas`"}} python/booleans -.-> lab-65455{{"`Text Data Handling in Pandas`"}} python/conditional_statements -.-> lab-65455{{"`Text Data Handling in Pandas`"}} python/lists -.-> lab-65455{{"`Text Data Handling in Pandas`"}} python/tuples -.-> lab-65455{{"`Text Data Handling in Pandas`"}} python/importing_modules -.-> lab-65455{{"`Text Data Handling in Pandas`"}} python/standard_libraries -.-> lab-65455{{"`Text Data Handling in Pandas`"}} python/numerical_computing -.-> lab-65455{{"`Text Data Handling in Pandas`"}} python/data_analysis -.-> lab-65455{{"`Text Data Handling in Pandas`"}} python/build_in_functions -.-> lab-65455{{"`Text Data Handling in Pandas`"}} end

Store Text Data

In pandas, you can store text data in two ways: using an object dtype NumPy array or a StringDtype extension type. We recommend using StringDtype because it's safer and more specific than the generic object dtype.

import pandas as pd

## create a series with `object` dtype
s1 = pd.Series(["a", "b", "c"], dtype="object")

## create a series with `StringDtype`
s2 = pd.Series(["a", "b", "c"], dtype="string")

Use String Methods

Pandas provides a suite of string processing methods that make it easy to operate on string data. These methods automatically exclude missing/NA values.

s = pd.Series(
    ["A", "B", "C", "Aaba", "Baca", np.nan, "CABA", "dog", "cat"], dtype="string"
)

## convert to lowercase
s.str.lower()

## convert to uppercase
s.str.upper()

## calculate the length of each string
s.str.len()

Extract Substrings

You can extract substrings using regular expressions. The extract method accepts a regular expression with at least one capture group.

## extract the first digit from each string
s = pd.Series(["a1", "b2", "c3"], dtype="string")
s.str.extract(r"(\d)", expand=False)

Test for Strings

You can check whether elements contain or match a pattern using the contains and match methods respectively.

## check if each string contains the pattern "a"
s.str.contains("a", na=False)

## check if each string matches the pattern "a"
s.str.match("a", na=False)

Create Dummy Variables

You can create dummy variables from string data using the get_dummies method.

## create dummy variables
s = pd.Series(["a", "a|b", np.nan, "a|c"], dtype="string")
s.str.get_dummies(sep="|")

Summary

In this lab, we learned how to handle text data in pandas. We learned how to store text data, use string methods for data preprocessing and transformation, extract substrings using regular expressions, test whether elements contain or match a pattern, and create dummy variables. These techniques are fundamental for processing text data for machine learning algorithms.

Other Python Tutorials you may like