Having a column renaming problem with pandas

As the title says I have a column renaming problem and it is about the following code:
(I am sorry for not pasting my project here but I do a lot of testings inside and it would look like a mess, but this code is of the project “Petal Power Inventory”)

import codecademylib3 import pandas as pd inventory = pd.read_csv("inventory.csv") print(inventory.head(10)) staten_island = inventory[inventory.location == "Staten Island"] product_request = staten_island["product_description"] product_request.rename(columns={"product_description": "products"}, inplace=True) print(product_request) #This should rename product_description to products (but it just changes the column name to "0")

I know codebyte won’t work here so I rather explain my error (even though there is no error message)

The problem is the following:
When I rename the columns of the original dataframe aka “inventory” it goes all fine, even if I’d do so with the exact same code. But when I try to rename the column in “product_request” which is derived from inventory, I cannot rename the column… It changes the column name from “product_description” to “0” (zero). I am clueless why it does that.
I’m guess it must do with the fact that it’s derived from another dataframe but I don’t know how to fix it…

(This is not part of the exercises of the project, I just wanted to do some plus work)

I’d appreciate it if somebody could explain it to me :sweat_smile:

Hi,
I think it could be line 8. You need two sets of square brackets around the column name, i.e.

product_request = staten_island[["product_description"]]

or the new df doesn’t have column names

Though I think that should give you an error if you try to rename them, so maybe there’s something else going on.

Hope that helps.

1 Like

You were right, the problem was with the bracket. Thank you very much!! :smile:

1 Like