FAQ: Modifying DataFrames - Performing Column Operations

This community-built FAQ covers the “Performing Column Operations” exercise from the lesson “Modifying DataFrames”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Data Analysis with Pandas

FAQs on the exercise Performing Column Operations

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello.
Tried same code as in this excercise on IDLE. There is an error

ImportError: cannot import name ‘lower’ from ‘string’ (C:\Miniconda\lib\string.py)

What can I do to get rid of this?
Thank you

1 Like

I have the same problem. I see you haven’t got an answer yet though…

I had the same problem. I have written simply:

df[‘Lowercase Name’] = df.Name.str.lower()

1 Like

I searched a little about this. This is probably a problem with the difference between Python 2 and Python 3. The lower is one of the deprecated string functions in Python 2. Instead, the one defined as String Methods is recommended. In Python 3, the lower is removed from the string library. So in Python 3, the code from string import lower don’t work and we need to use str.lower() as you’ve suggested.

3 Likes

As a way along with this exercise that uses the .apply() method, we can also simply replace lower with str.lower:

df['Lowercase Name'] = df.Name.apply(str.lower)

Now we don’t need to import string.

The exercise demonstrated passing upper and lower functions through columns in Pandas - but what if I simply want to follow conventional English syntax, with the first letter of first and last names capitalized?

1 Like

In that case you can use str.title().

Does anyone know how to perform an operation on a column with a name consisting of multiple words?

This one works perfectly fine:

df[‘Lowercase Name’] = df.Name.apply(lower)

This one doesn’t work. How can I fix it?

df[‘Uppercase Name’] = df.[‘Lowercase Name’].apply(upper)

Thank you in advance!

1 Like

How do I write the .apply() method if the column name has a space?
Does
df[‘Name’] = df.Name.apply(upper)
become
df[‘Name’] = df[‘Name’](upper) or something…?

1 Like

Simply replace df.Name with df['Name'].

df['Name'] = df['Name'].apply(upper)

In addition, the upper is one of the deprecated string functions. In Python 3, it is removed from the string library. I recommend using str.upper instead.

1 Like

Remove the dot just after the df on the right side.

df['Uppercase Name'] = df['Lowercase Name'].apply(upper)
1 Like
df['Lowercase Name'] = df.Name.apply(str.lower)

Why does the code editor display the dot between ‘Name’ and ‘apply’ as orange as opposed to white?

Does anyone know why we’re using lower and upper without the parentheses it usually is used with? It wouldn’t even work when i tried str.lower()

@cahira welcome to the forums!

From my limited understanding, str.lower is the object method and () is the operator.

Here, you are using the apply function to tell Python to apply the .lower method to each of the strings within the column. You are not doing .lower() directly because the column itself is not a string. You are applying the .lower to the string content of the columns.

1 Like