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