Using String Methods in Pandas
This introduction to pandas is derived from Data School's pandas Q&A with my own notes and code.
Using string methods in pandas¶
In [1]:
# convert string to uppercase in Python
'hello'.upper()
Out[1]:
How about string methods in pandas?
There are many!
In [2]:
import pandas as pd
In [3]:
url = 'http://bit.ly/chiporders'
orders = pd.read_table(url)
In [4]:
orders.head()
Out[4]:
Making the item_name uppercase
In [5]:
# .str is a string method
orders.item_name.str.upper()
Out[5]:
In [6]:
# you can overwrite with the following code
orders.item_name = orders.item_name.str.upper()
In [7]:
orders.head()
Out[7]:
Check presence of substring
This is useful to filter data
In [9]:
orders.item_name.str.contains('Chicken').head()
Out[9]:
Chain string methods
In [11]:
# replacing elements
orders.choice_description.str.replace('[', '').head()
Out[11]:
In [13]:
# chain string methods
orders.choice_description.str.replace('[', '').str.replace(']', '').head()
Out[13]:
In [16]:
# using regex to simplify the code above
orders.choice_description.str.replace('[\[\]]', '').head()
Out[16]: