Chapter 22. Vectorized String Operations
One strength of Python is its relative ease in handling and manipulating string data. Pandas builds on this and provides a comprehensive set of vectorized string operations that are an important part of the type of munging required when working with (read: cleaning up) real-world data. In this chapter, we’ll walk through some of the Pandas string operations, and then take a look at using them to partially clean up a very messy dataset of recipes collected from the internet.
Introducing Pandas String Operations
We saw in previous chapters how tools like NumPy and Pandas generalize arithmetic operations so that we can easily and quickly perform the same operation on many array elements. For example:
In
[
1
]:
import
numpy
as
np
x
=
np
.
array
([
2
,
3
,
5
,
7
,
11
,
13
])
x
*
2
Out
[
1
]:
array
([
4
,
6
,
10
,
14
,
22
,
26
])
This vectorization of operations simplifies the syntax of operating on arrays of data: we no longer have to worry about the size or shape of the array, but just about what operation we want done. For arrays of strings, NumPy does not provide such simple access, and thus you’re stuck using a more verbose loop syntax:
In
[
2
]:
data
=
[
'peter'
,
'Paul'
,
'MARY'
,
'gUIDO'
]
[
s
.
capitalize
()
for
s
in
data
]
Out
[
2
]:
[
'Peter'
,
'Paul'
,
'Mary'
,
'Guido'
]
This is perhaps sufficient to work with some data, but it will break if there are any missing values, so this approach requires putting in extra checks:
In
[
3
]:
data
=
[
'peter'
,
'Paul'
,
None
,
'MARY'
,
'gUIDO' ...
Get Python Data Science Handbook, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.