2

I am writing a function that recodes Pandas DataFrame index objects from a recode dictionary.

recodes = {'sales' : 'CurrentSales', 'cash' : 'US$' }
  1. Is there any built in method for this?
  2. What is an efficient way of doing this?

Using an example:

import pandas as pd
import io

text = '''\
STK_ID RPT_Date sales cash
000568 20120930 80.093 57.488
000596 20120930 32.585 26.177
000799 20120930 14.784 8.157
'''

df = pd.read_csv(io.BytesIO(text), delimiter = ' ', 
                 converters = {0:str})
df.set_index(['STK_ID','RPT_Date'], inplace = True)

enter image description here

What I have implemented is essentially:

nw_idx = ['']*len(df.columns)
for key in recodes.keys():
    for idx, colname in enumerate(df.columns):
        if colname == key:
            nw_idx[idx] = recodes[key]

df.columns = pd.Index(nw_idx)

enter image description here

sanguineturtle
  • 1,425
  • 2
  • 15
  • 29
  • I would consider yours a dupe of [this](http://stackoverflow.com/questions/11346283/renaming-columns-in-pandas) question, but the accepted answer there is not what I would use. Still, the [higher voted answers](http://stackoverflow.com/a/11354850/507762) there apply to your situation. – U2EF1 Jul 22 '14 at 01:37

1 Answers1

3
df.rename(columns={'sales' : 'CurrentSales', 'cash' : 'US$' })
U2EF1
  • 12,907
  • 3
  • 35
  • 37
  • Oh Wow - that was simple. Thanks. I was searching the api on actual index objects ``df.columns`` etc. – sanguineturtle Jul 22 '14 at 01:38
  • @sanguineturtle You can also pass this function a list of new names, or a function (like `str.lower`). And you can assign to `df.columns` directly. Index objects have a `name` field, and can get more confusing. – U2EF1 Jul 22 '14 at 01:39
  • It would be nice if rename could produced a "report". I might write a wrapper for adding a report for converted items, and not converted items etc. – sanguineturtle Jul 22 '14 at 01:45