BLACK FRIDAY EXTRA SAVINGS EVENT - EXTENDED
START FREE

Exploring_Ebay_Car_Sales_Data - pd.series.map(Dict) not working properly

Hi, I normally use a local jupyter notebook/anaconda installation to test and debug Python code before uploading it to the dataquest, and the code below is working fine on my local env. However, on dataquest, I get an error.

I’m stuck at.

https://app.dataquest.io/m/294/guided-project%3A-exploring-ebay-car-sales-data/2/cleaning-column-names

# Importing pandas library.
import pandas as pd
# Opening the dataset.
autos = pd.read_csv('autos.csv',encoding='Latin-1')
# Converting the columns names from camelcase into snakecase style.
# Copying the dataframe.
autos_copy = autos.copy()
columns = autos_copy.columns
# Dictionary to map old names to new names.
mapping_columns = {
'dateCrawled':'date_crawled',
'name':'name',
'seller':'seller',
'offerType':'offer_type',
'price':'price',
'abtest':'abtest',
'vehicleType':'vehicle_type',
'yearOfRegistration':'registration_year',
'gearbox':'gearbox',
'powerPS':'power_ps',
'model':'model',
'odometer':'odometer',
'monthOfRegistration':'registration_month',
'fuelType':'fuel_type',
'brand':'brand',
'notRepairedDamage':'unrepaired_damage',
'dateCreated':'ad_created',
'nrOfPictures':'nr_of_pictures',
'postalCode':'postal_code',
'lastSeen':'last_seen'}
# Updating the column names.
autos_copy.columns = columns.map(mapping_columns)
autos_copy.head()

What I expected to happen: Rename the columns.

What actually happened: Got an error.

TypeError: 'dict' object is not callable

hey @italo.moises

Welcome to DataQuest Community!

I ran this code in jupyter notebook. It seems to execute fine. Could you please share your notebook, if you are still getting the error?

you may refer to this post for upload.

Guided_Project_Exploring_Ebay_Car_Sales_Data.ipynb (7.4 KB)

After downloading it I executed the same Jupyter file locally and didn’t get any error, oddly I executed it all over again on Dataquest Jupyter env and got the same error as before.

Click here to view the jupyter notebook file in a new tab

That’s a head-scratcher! I am able to replicate your error in the platform, @italo.moises . My best guess is that this is due to a difference between the Pandas version used on the DQ platform and in your local environment. DQ uses Pandas 0.22.0. I have Pandas 1.0.0 on my machine and your code works fine there.

I’m not sure, but I’m thinking the issue is that .map() can’t be used on columns this way because it’s an Index. It seems like later versions it’s fine, but doesn’t appear so on 0.22.0.

I was able to get it to work on the platform by making columns into a Series:

autos_copy.columns = pd.Series(columns).map(mapping_columns)
3 Likes

Thank you, nice that I have learned two ways to make the same thing :wink:. I double-checked my code and the command print(type(columns)) returned <class ‘pandas.core.indexes.base.Index’>, I haven’t noticed that it didn’t return a Series type.

Even I found the error for using following code:
autos_copy.columns = columns.map(mapping_columns)

However, the problem does not appear on Local Machine. And, I feel it is due to the version differences of Pandas.

Therefore, I used following code:
DataFrame.rename({oldcolumnname:newcolumnname})

Thank you so muth! I worked more than one hour to try resolve this bug and your answer was very helpfull!

Hello
To be honest it’s not the best approach to be but it was too interesting so I tried to make function to change all camel case into snake case:

def change_column(name):
    new_name = ''
    for char in name:
        if char.islower() or char == '_':
            new_name = new_name + char
        else:
            new_name = new_name + '_' + char.lower()
    return new_name
autos.columns = [change_column(column) for column in autos.columns]
1 Like

I did the same method by creating a dictionary, but used this pandas method. It worked well without any issues:

autos = autos.rename(mapper = mapping_dictionary, axis = 1)

2 Likes