Table of Contents
In Python, a Pandas Series is a one-dimensional labeled array capable of holding data of any type. A Pandas Series can be compared to a column in an Excel spreadsheet. If you have a Series containing string values that you want to make all lowercase, calling `lower()` on the Series object directly will raise an AttributeError: 'Series' object has no attribute 'lower'
.
To properly format string data in a Pandas Series, use the `str.lower()` accessor. For example:
df['Strings'] = df['Strings'].str.lower()
You can also use the `apply()` method with a lambda function, for example:
df['String'] = df['Strings'].apply(lambda x: x.lower())
AttributeError: ‘Series’ object has no attribute ‘lower’
AttributeError occurs in Python when you try to access an attribute or method that does not exist for a particular object. The error “‘Series’ object has no attribute ‘lower’” indicates that Pandas Series objects do not support lower()
directly.
Example
Here’s an example where we attempt to use lower()
on a Pandas Series to convert its string values to lowercase:
import pandas as pd
series = pd.Series(['LeARNINg', 'PYTHON', 'iS', 'ReALly', 'FUN!'])
print(series)
0 LeARNINg
1 PYTHON
2 iS
3 ReALly
4 FUN!
dtype: object
Attempting to use `lower()` directly on the Series will produce an AttributeError:
series_lower = series.lower()
print(series_lower)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-4-667647a019d5> in <module>
----> 1 series_lower = series.lower()
2 print(series_lower)
~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/generic.py in __getattr__(self, name)
5581 ):
5582 return self[name]
-> 5583 return object.__getattribute__(self, name)
5584
5585 def __setattr__(self, name: str, value) -> None:
AttributeError: 'Series' object has no attribute 'lower'
The AttributeError occurs because we are trying to use the string method lower()
on a Pandas Series object. The Series equivalent for lower()
is str.lower()
.
Solution #1: Use str.lower()
To solve this error, use str.lower()
to convert the string values to lowercase. Here’s the revised code:
series_lower = series.str.lower()
print(series_lower)
0 learning
1 python
2 is
3 really
4 fun!
dtype: object
Solution #2: Use apply()
Alternatively, you can use apply()
to call lower()
on each string in the Series. Here’s how:
series_lower = series.apply(lambda x: x.lower())
print(series_lower)
0 learning
1 python
2 is
3 really
4 fun!
dtype: object
Summary
The AttributeError 'Series' object has no attribute 'lower'
occurs when trying to call the string method lower()
on a Pandas Series object. To solve this, use either str.lower()
or apply()
with lower()
to convert each string in the Series to lowercase.
For more on using Pandas with string functions, visit:
- How to Solve Python AttributeError: ‘str’ object has no attribute ‘contains’
- How to Solve Python ValueError: Can only compare identically-labeled DataFrame objects
- How to Solve Python AttributeError: module ‘pandas’ has no attribute ‘scatter_matrix’
- How to Solve Python AttributeError: ‘Series’ object has no attribute ‘colNames’ using apply()
For more on data science and machine learning with Python, check out our online Python courses.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.