How do I use string methods in pandas?

Data School · Beginner ·📰 AI News & Updates ·10y ago

Key Takeaways

This video teaches how to use string methods in pandas to manipulate strings in a Series

Full Transcript

hello and welcome back to my Q&A video series about the pandas library in Python and the question for today is how do I use string Methods in pandas all right so in Python we're used to having access to lots of string Methods for example if I have the string hello and I want to make it uppercase I just use the string method upper Okay so uh there you actually have access to a lot of string Methods in pandas you just have to know how to find them so let's uh pull up an example data set so first we import pandas aspd and our data set for today is uh online orders from the Chipotle restaurant chain so I'm going to say orders equals pd. readable and pass the URL bit. Le chip orders okay and here are the first five rows of the data frame and so let's pretend we want to make the item name uppercase okay so um you might think oh this would be a series method and I would just say something like orders do item name. uper and you've almost got it right but that's not going to work if you want to use a string method on a panda series you actually use you say st. uper so that's saying it's a string method and when we do that you can see that we've gotten back the uppercase versions of each of the item names and if you like you could overwrite the original uh item name series with these values okay so uh let's look at another method as an example so another string method is contains so this is how I check for the presence of a substring so I could say uh Str str. contains and I'm going to look for chicken okay and when I run that I get back a series of booing a series of trues and falses indicating whether a particular item in this series contained chicken so we see false false false false true and that's because this has uh Chicken in it and you might ask well why would we uh use that well we might want to filter the data frame like this and only look at um uh rows in the data frame that have chicken in the item name okay so there are tons of string Methods you have access to um what you'll want to look at is the pandas API reference so I'll link to this in the description below but you can just Google pandas API reference and you'll get to this page and once you get here you'll want to search for string handling okay and find this section and then you'll want to you'll be able to see all the different string Methods you have access to okay all right uh as always I'm going to end with a bonus and in fact I have two bonuses for today and the first bonus is just to show you that you can chain together uh string Methods okay so let's say that I want to remove the bracket characters from these strings in Choice description okay okay so if I said orders. Choice description. st. replace and then the way replace works I say I want to replace a left bracket with nothing okay if I run that it indeed removes the left bracket okay from all every element in the series what if I also want to remove the right bracket well I can actually just change chain this with another replace method so remember this returns a pandis series and as such I can run another series method on it so I just say. st. replace and this time I want to replace the right bracket with nothing and there you go okay so that's the first bonus the second bonus is just to tell you that um many panda string Methods actually will accept regular Expressions okay so uh if you don't know regular Expressions this won't make much sense but um I'm going to write that previous line of code uh more simply with a regular expression so I want to say I'm just going to define a character class you know so using brackets that defines my character class and then I'm saying uh I want to replace either a left bracket or a right bracket uh with nothing so actually it looks a little funny but I put brackets in there I have to escape both of them but when I run that um it indeed does work in replacing the left and right brackets okay so that was using regular Expressions all right well that is it for today thanks as as always for joining me feel free to click subscribe if you'd like to see more videos like this uh feel free to leave a question or a tip in the comments below and that way we can all learn from one another and that is it so I hope to see you again soon

Original Description

pandas includes powerful string manipulation capabilities that you can easily apply to any Series of strings. In this video, I'll show you how to access string methods in pandas (along with a few examples), and then end with two bonus tips to help you maximize your efficiency. SUBSCRIBE to learn data science with Python: https://www.youtube.com/dataschool?sub_confirmation=1 JOIN the "Data School Insiders" community and receive exclusive rewards: https://www.patreon.com/dataschool == RESOURCES == GitHub repository for the series: https://github.com/justmarkham/pandas-videos string handling documentation: http://pandas.pydata.org/pandas-docs/stable/api.html#string-handling == LET'S CONNECT! == Newsletter: https://www.dataschool.io/subscribe/ Twitter: https://twitter.com/justmarkham Facebook: https://www.facebook.com/DataScienceSchool/ LinkedIn: https://www.linkedin.com/in/justmarkham/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Data School · Data School · 33 of 60

1 Setting up Git and GitHub
Setting up Git and GitHub
Data School
2 Navigating a GitHub Repository - Part 1
Navigating a GitHub Repository - Part 1
Data School
3 Forking a GitHub Repository
Forking a GitHub Repository
Data School
4 Creating a New GitHub Repository
Creating a New GitHub Repository
Data School
5 Copying a GitHub Repository to Your Local Computer
Copying a GitHub Repository to Your Local Computer
Data School
6 Committing Changes in Git and Pushing to a GitHub Repository
Committing Changes in Git and Pushing to a GitHub Repository
Data School
7 Syncing Your GitHub Fork
Syncing Your GitHub Fork
Data School
8 Allstate Purchase Prediction Challenge on Kaggle
Allstate Purchase Prediction Challenge on Kaggle
Data School
9 Troubleshooting: Updates Rejected When Pushing to GitHub
Troubleshooting: Updates Rejected When Pushing to GitHub
Data School
10 Hands-on dplyr tutorial for faster data manipulation in R
Hands-on dplyr tutorial for faster data manipulation in R
Data School
11 ROC Curves and Area Under the Curve (AUC) Explained
ROC Curves and Area Under the Curve (AUC) Explained
Data School
12 Going deeper with dplyr: New features in 0.3 and 0.4 (tutorial)
Going deeper with dplyr: New features in 0.3 and 0.4 (tutorial)
Data School
13 What is machine learning, and how does it work?
What is machine learning, and how does it work?
Data School
14 Setting up Python for machine learning: scikit-learn and Jupyter Notebook
Setting up Python for machine learning: scikit-learn and Jupyter Notebook
Data School
15 Getting started in scikit-learn with the famous iris dataset
Getting started in scikit-learn with the famous iris dataset
Data School
16 Training a machine learning model with scikit-learn
Training a machine learning model with scikit-learn
Data School
17 Comparing machine learning models in scikit-learn
Comparing machine learning models in scikit-learn
Data School
18 Data science in Python: pandas, seaborn, scikit-learn
Data science in Python: pandas, seaborn, scikit-learn
Data School
19 Selecting the best model in scikit-learn using cross-validation
Selecting the best model in scikit-learn using cross-validation
Data School
20 How to find the best model parameters in scikit-learn
How to find the best model parameters in scikit-learn
Data School
21 How to evaluate a classifier in scikit-learn
How to evaluate a classifier in scikit-learn
Data School
22 What is pandas? (Introduction to the Q&A series)
What is pandas? (Introduction to the Q&A series)
Data School
23 How do I read a tabular data file into pandas?
How do I read a tabular data file into pandas?
Data School
24 How do I select a pandas Series from a DataFrame?
How do I select a pandas Series from a DataFrame?
Data School
25 Why do some pandas commands end with parentheses (and others don't)?
Why do some pandas commands end with parentheses (and others don't)?
Data School
26 How do I rename columns in a pandas DataFrame?
How do I rename columns in a pandas DataFrame?
Data School
27 How do I remove columns from a pandas DataFrame?
How do I remove columns from a pandas DataFrame?
Data School
28 How do I sort a pandas DataFrame or a Series?
How do I sort a pandas DataFrame or a Series?
Data School
29 How do I filter rows of a pandas DataFrame by column value?
How do I filter rows of a pandas DataFrame by column value?
Data School
30 How do I apply multiple filter criteria to a pandas DataFrame?
How do I apply multiple filter criteria to a pandas DataFrame?
Data School
31 Your pandas questions answered!
Your pandas questions answered!
Data School
32 How do I use the "axis" parameter in pandas?
How do I use the "axis" parameter in pandas?
Data School
How do I use string methods in pandas?
How do I use string methods in pandas?
Data School
34 How do I change the data type of a pandas Series?
How do I change the data type of a pandas Series?
Data School
35 When should I use a "groupby" in pandas?
When should I use a "groupby" in pandas?
Data School
36 How do I explore a pandas Series?
How do I explore a pandas Series?
Data School
37 How do I handle missing values in pandas?
How do I handle missing values in pandas?
Data School
38 What do I need to know about the pandas index? (Part 1)
What do I need to know about the pandas index? (Part 1)
Data School
39 What do I need to know about the pandas index? (Part 2)
What do I need to know about the pandas index? (Part 2)
Data School
40 How do I select multiple rows and columns from a pandas DataFrame?
How do I select multiple rows and columns from a pandas DataFrame?
Data School
41 Machine Learning with Text in scikit-learn (PyCon 2016)
Machine Learning with Text in scikit-learn (PyCon 2016)
Data School
42 When should I use the "inplace" parameter in pandas?
When should I use the "inplace" parameter in pandas?
Data School
43 How do I make my pandas DataFrame smaller and faster?
How do I make my pandas DataFrame smaller and faster?
Data School
44 How do I use pandas with scikit-learn to create Kaggle submissions?
How do I use pandas with scikit-learn to create Kaggle submissions?
Data School
45 More of your pandas questions answered!
More of your pandas questions answered!
Data School
46 How do I create dummy variables in pandas?
How do I create dummy variables in pandas?
Data School
47 How do I work with dates and times in pandas?
How do I work with dates and times in pandas?
Data School
48 How do I find and remove duplicate rows in pandas?
How do I find and remove duplicate rows in pandas?
Data School
49 How do I avoid a SettingWithCopyWarning in pandas?
How do I avoid a SettingWithCopyWarning in pandas?
Data School
50 How do I change display options in pandas?
How do I change display options in pandas?
Data School
51 How do I create a pandas DataFrame from another object?
How do I create a pandas DataFrame from another object?
Data School
52 How do I apply a function to a pandas Series or DataFrame?
How do I apply a function to a pandas Series or DataFrame?
Data School
53 Getting started with machine learning in Python (webcast)
Getting started with machine learning in Python (webcast)
Data School
54 Q&A about Machine Learning with Text (online course)
Q&A about Machine Learning with Text (online course)
Data School
55 Your pandas questions answered! (webcast)
Your pandas questions answered! (webcast)
Data School
56 Machine Learning with Text in scikit-learn (PyData DC 2016)
Machine Learning with Text in scikit-learn (PyData DC 2016)
Data School
57 Write Pythonic Code for Better Data Science (webcast)
Write Pythonic Code for Better Data Science (webcast)
Data School
58 Web scraping in Python (Part 1): Getting started
Web scraping in Python (Part 1): Getting started
Data School
59 Web scraping in Python (Part 2): Parsing HTML with Beautiful Soup
Web scraping in Python (Part 2): Parsing HTML with Beautiful Soup
Data School
60 Web scraping in Python (Part 3): Building a dataset
Web scraping in Python (Part 3): Building a dataset
Data School

Related Reads

Up next
Masterplan Content Update – Juni 2026
Masterplan.com
Watch →