Gustavo
Gustavo

Reputation: 41

Why I am receiving the error Error: module ‘string’ has no attribute ‘lower’ when I am using Pandas?

I am new in coding. I am doing the data science path, currently in module introduction to pandas.

I download Jupiter Notebook using Anaconda.

I tried to change all the strings of one column to uppercase or lowercase, but I received the error: “AttributeError: module ‘string’ has no attribute ‘lower’.”

I tried with “upper” instead of “lower” and I received the same error.

I tried: from string import upper and returns: ImportError: cannot import name ‘upper’ from ‘string’ (C:\Users\gustavo\anaconda3\lib\string.py)

Then I tried import string and I didn’t receive any error but when I tried: df[‘estado’] = df.estado.apply[string.upper], I have the error: AttributeError: module ‘string’ has no attribute ‘upper’.

I have checked many videos on youtube and I have googled the error but I don’t know how to resolve it.

Can someone help me?

Upvotes: 3

Views: 3020

Answers (4)

Garett
Garett

Reputation: 7

This method appears to have been deprecated after Python 2.7, as it appears in the documentation for string in Python 2.7, but not in the 3.5 documentation or later.

In this case, you no longer need to perform an import. The code to perform your action, at least for Python 3.9, is as follows:

df['estado'] = df.estado.apply(str.upper)

Upvotes: 0

Ken Seehart
Ken Seehart

Reputation: 412

string.upper and string.lower were in python2. They have been removed from python3, so the import fails.

There are a few options:

If you want a one line drop in fix, you can replace

from string import lower

with this:

lower = str.lower

Better solution is modify your usage everywhere to use methods instead of functions, so replace lower(s) with s.lower().

Bigger picture is that the error you encountered indicates that you may be working with code that needs to be ported to python3. See https://docs.python.org/3/howto/pyporting.html

Upvotes: 3

Jonas
Jonas

Reputation: 1769

To lowercase a pandas column you can use .str.lower(), as seen in the documentation (Vectorized string methods):

df['columname'] = df['columname'].str.lower() 

If you want to lowercase the whole dataset you can use apply() in combination with asytpe():

df = df.apply(lambda x: x.astype(str).str.lower())

Upvotes: 1

HarryS
HarryS

Reputation: 312

You need to give a pair of parenthesis, since string.upper() is a function:

df[‘estado’] = df.estado.apply[string.upper()]

Upvotes: 0

Related Questions