Ludo
Ludo

Reputation: 2527

pandas df.rename on both indices and columns not always working

I have a list of dataframes and I iterate over them, renaming all the columns/indices using one pandas df.rename method call.

df.rename(
    {
        'vol': 'Volume Sales',
        'val': 'Value Sales',
    },
    index={
        't1': info['literal_periods'][0],
        't2': info['literal_periods'][1],
        'acv': '% ACV Distribution',
        'aic': 'Average Items Carried',
        'tdp': 'Total Distribution Points',
        'vol': 'Volume Sales',
        'psl': 'Promo Sales',
        'Share of AIC': '{} share of {} AIC'.format(
            info['name'], info['p1']),
        'Share of TDP': '{} share of {} TDP'.format(
            info['name'], info['p1']),
        'Target Product': info['name'],
        'target product': info['name'],
    },
    columns={
        't1':
        info['literal_periods'][0],
        't2':
        info['literal_periods'][1],
        'promo change':
        '% change from ya',
        'non promo change':
        '% change from ya',
        'sales change':
        '% change from ya',
        'val':
        'Value Sales (£)',
        'vol':
        'Volume Sales (L)',
        'volsu':
        'Volume Sales (units)',
        'litres per unit':
        'litres/unit',
        't2 Promo Sales':
        '{} Promo Sales'.format(info['literal_periods'][1]),
        't2 Non-Promo Sales':
        '{} Non Promo Sales'.format(info['literal_periods'][1]),
        't2 Total Sales':
        '{} Total Sales'.format(info['literal_periods'][1])
    },
    inplace=True)

It seems to work perfectly for some tables, partially for others and then not at all for some. Why? For example the 'aic' index is not being renamed properly in a df with 'aic' in it even though the column 'vol' is correctly being mapped to 'Volume Sales' in the same df.

Upvotes: 3

Views: 5965

Answers (2)

Ben.T
Ben.T

Reputation: 29635

According to Pandas documentation on rename where parameters mapper, index, columns are explained, it's written:

Use either mapper and axis to specify the axis to target with mapper, or index and columns

In what you give as example,

{
    'vol': 'Volume Sales',
    'val': 'Value Sales',
}

which is understood by the function as mapper = and then you give index and columns. Without being able to reproduce, it's possible that the function rename has some "troubles" to understand the parameters you give (I guess looking at the code behind would give the reason why)

This also explains why the column 'vol' is rename 'Volume Sales' and not 'Volume Sales (L)' as define in column=

Upvotes: 5

Ludo
Ludo

Reputation: 2527

The problem was in the first few lines:

df.rename(
    {
        'vol': 'Volume Sales',
        'val': 'Value Sales',
    },

I'm not sure why, but once I removed the first two mappings it started working. My guess is that the way df.rename works is if you pass a general mapping and it finds a term within your df matching one of the keys, then it will only look for those and not care for your specific index and columns mappings.

Upvotes: 1

Related Questions