The lower() Method with Pandas Series
Let's find out how to use lower with a pandas series.
We'll cover the following
Try it yourself
Try executing the code below to see the result.
import pandas as pds = pd.Series(['Rick', 'Morty', 'Summer', 'Beth', 'Jerry'])print(s.lower())
Explanation
The pandas.Series
has a lot of methods, like those listed below:
In [1]: import pandas as pd
In [2]: sum(1 for attr in dir(pd.Series) if attr[0] != '_')
Out[2]: 207
But, lower
is not one of them, as we can see below:
In [3]: hasattr(pd.Series, 'lower')
Out[3]: False
Most of the time, people use pandas with numerical data. The pandas developers decided to move non-numerical methods out of the already big pandas.Series
top-level API. To make the teaser code work, we can use the .str
attribute.
The pandas.Series
(and pandas.DataFrame
) has several special attribute accessors, like those listed below:
.str
for string methods such aslower
andmatch
..dt
to work with date time and timestamp data (for example,s.dt.year
)..cat
to work with categorical data.sparse
to work with sparse data
Solution
import pandas as pds = pd.Series(['Rick', 'Morty', 'Summer', 'Beth', 'Jerry'])print(s.str.lower())
Get hands-on with 1300+ tech skills courses.