pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
43.68k stars 17.92k forks source link

df.rename not working with list of dataframes #49695

Closed ua-chjb closed 1 year ago

ua-chjb commented 1 year ago

I have a list of dataframes, df1,df2 ... df6. I want to rename all columns in these dataframes in the same way. However, when I run through it with a for list, the columns are not changed.

Here is my before dataframe: exp_dt1_example

My code:

for dataframe in [exp_ch_dt1,exp_egg_dt1,exp_ff_dt1,exp_fulfam_dt1,exp_ir_dt1,exp_rr_dt1,
                  exp_ch_dt2,exp_egg_dt2,exp_ff_dt2,exp_fulfam_dt2,exp_ir_dt2,exp_rr_dt2]:
    dataframe = dataframe.T.rename(columns={
    'Television_exp':'Television',
    'Radio_exp':'Radio',
    'Billboards_exp':'Billboards',
    'Retail: Lottery signs/posters at retail stores_exp':'Retail: Lottery signs/posters at retail stores',
    'Internet/online_exp':'Internet/online',
    'Newspapers_exp':'Newspapers',
    'Bus shelters_exp':'Bus shelters',
    'Subway_exp':'Subway',
    'Commuter train_exp':'Commuter train',
    'Retail: Digital screens on monitors at retail stores_exp':'Retail: Digital screens on monitors at retail stores',
    'Pay phone kiosks_exp':'Pay phone kiosks',
    'Movie Theaters_exp':'Movie Theaters',
    'Buses_exp':'Buses',
    'Live Newscasts_exp':'Live Newscasts',
    'Live TV Drawings_exp':'Live TV Drawings',
    'Retail: Employees at the store_exp':'Retail: Employees at the store',
    'Family or friends_exp':'Family or friends',
    'New York Lottery website_exp':'New York Lottery website',
    'Social media (Facebook, Twitter)_exp':'Social media (Facebook, Twitter)',
    'New York Lottery app_exp':'New York Lottery app',
    'Retail: Retail Location_exp':'Retail: Retail Location',
    'Email newsletter_exp':'Email newsletter',
    'Retail: I see the new tickets when I am at the checkout_exp':'Retail: I see the new tickets when I am at the checkout',
    'Other (please specify)_exp':'Other (please specify)'
  }).T

And my after dataframe: exp_dt1_example2

They column names are exactly the same. Can anyone tell me why df.rename isn't working?

MarcoGorelli commented 1 year ago

Hi - please post a reproducible example, see

https://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports

for how

Also, please don't post screenshots as they're not searchable https://youtu.be/svlsnZFWWDE

MarcoGorelli commented 1 year ago

Actually, let's try again 😄 Please open a new issue, and don't skip the template this time