Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Introduction to pandas Exploring pandas Optional Challenge #2 - Update Users

renaming one index in pandas DataFrame (jeffrey username)

In this Optional Challenge #2, I can't figure out how to rename an index in a pandas DataFrame row. The idea is that 'jefrey' is the proper spelling, not 'jeffrey'

users.loc['jeffrey']
jef = users.index == 'jeffrey'

# this didn't work -- attempt at creating a new row with correct name
users.loc['jefrey'] = users.loc[jef]

# this didn't work either
users[jef].index = 'jefrey'

Would I use pandas.Index.rename ? (Not sure if we learned it in this course)

2 Answers

Renaming an index is the same way as you rename a column.

Try this:

users.rename(index={'jeffrey': 'jefrey'}, inplace=True)

To check that the index name has changed:

users.loc['jefrey']

thank you Putra!! it makes absolute sense!

Shahriyar Ahmed
Shahriyar Ahmed
1,701 Points

Ran into the same problem a year later. I used the rename method but didnt include inplace =True. Thank you @Putra Aryotama

Arturo Acosta
Arturo Acosta
7,635 Points

I did this, actually when i check for both results myself i get jefrey and kimberly's last name Deal as the challenge says but somehow the test shows as if i have errors, but for the purpose of the challenge i did update said values

          ## CHALLENGE - Update users ##

# TODO: Update kimberly@yahoo.com to have the last name of "Deal"

user = users.index[users['email']=='kimberly@yahoo.com']
target1 = user[0]
users.loc[target1,'last_name']='Deal'

# TODO: Update the username jeffrey to jefrey (only one f)
users.rename(index={'jeffrey':'jefrey'},inplace=True)

users.loc[target1], users.loc['jefrey']

#SORRY FOR THIS I DONT KNOW HOW TO SHOW THE OUTPUT ANY OTHER WAY BUT THE OUTPUT READS AS BELOW

(first_name                  Kimberly
 last_name                       Deal
 email             kimberly@yahoo.com
 email_verified                 False
 signup_date               2018-01-06
 referral_count                     5
 balance                        54.73
 Name: kimberly, dtype: object, first_name                        Jeffrey
 last_name                         Stewart
 email             stewart7222@hotmail.com
 email_verified                       True
 signup_date                    2018-01-02
 referral_count                          0
 balance                             40.58
 Name: jefrey, dtype: object)
Flore W
Flore W
4,744 Points

I did a workaround without using the "rename" method, but I definitely like Putra's solution better :)

# define a new dictionary with the same key and values from Jeffrey
newdict = dict(first_name='Jeffrey', last_name='Stewart', email='stewart7222@hotmail.com', email_verified=True, signup_date=datetime.strptime('20180102', '%Y%m%d').strftime('%Y-%m-%d'), referral_count=0, balance=40.58)

# transform the dictionary into a panda series
newseries = pd.Series(newdict)

# name the series 'jefrey'
newseries.name='jefrey'

# append that series to the users dataframe
users = users.append(jefrey)

# get rid of the row 'jeffrey'
users.drop(index='jeffrey', inplace=True)

# Return the whole data frame
users