Display column name different from word dictionary name in Pandas?

I am new to Pandas and I see that there are many ways to change column headers. For example, the set_axis command works as follows:

 >>> import pandas as pd >>> import numpy as np >>> df = pd.DataFrame(np.arange(3),columns=['a']) >>> df a 0 0 1 1 2 2 >>> df["a"][0] 0 >>> df.set_axis(['A'],axis=1,inplace=True) >>> df A 0 0 1 1 2 2 >>> df["A"][0] 0 

EDIT: Alternatively, you can use

 df.columns = ['A'] 

to change the name of the column.

But now it seems that if I want to change the column heading for display purposes only (since the heading label is inconvenient to use as a dictionary key), I need to create a completely new data frame:

 >>> df_pretty = df.set_axis(['Long label (%)'],axis=1,inplace=False) df_pretty Long label (%) 0 0 1 1 2 2 

Is it correct? Or am I missing something? It seems like a waste of memory to recreate a new print-only data frame. I would have thought that Pandas would be able to store the internal "key" and a separate column label used only for display.

+2
source share
2 answers

If you first created a dictionary for converting from short names to long names:

 di = {'a':'long name for a'} 

Then it is very simple to use rename to display long names when you want:

 df.rename(di,axis=1) long name for a 0 0 1 1 2 2 

Note that this is for only one column, but as soon as you install the dictionary, the syntax will be as short for 100 columns as it is for 1.

You also do not need to make any permanent changes this way. Just add a renaming method when you want to display things differently. Or, alternatively, store long names in a constant data frame and just use a dictionary to display short names as needed.

Honestly, I don’t think it is more complicated than if the labels were saved as column metadata, since even then you often want to explicitly specify short or long names and for this you need some kind of keyword argument. Also, since python dictionaries are so flexible, you have many options: you can have short, medium, long names stored in dictionaries, and configure functions to automatically create short names from long names, etc.

+2
source

The solution posted by @JohnE looks like the best way.

I would also like to use a format string, and so add a few details here:

 import pandas df = pandas.DataFrame({'a' : [1,2,3],'b' : [4,5,6]}) di = {'a' : 'A (J/K*kg)', 'b' : 'B (N/m^2)'} fstr = {di["a"] : '{:6.2f}', di["b"]:'{:5.2e}'} df.rename(columns=di).style.format(fstr) 

This, made in a Juptyer laptop, looks perfect and does exactly what I want.

enter image description here

When I tried the same code in the Python tooltip, the stylist does not display.

 >>> import pandas >>> df = pandas.DataFrame({'a' : [1,2,3],'b' : [4,5,6]}) >>> di = {'a' : 'A (J/K*kg)', 'b' : 'B (N/m^2)'} >>> fstr = {di["a"] : '{:6.2f}', di["b"]:'{:5.2e}'} >>> df.rename(columns=di).style.format(fstr) <pandas.io.formats.style.Styler object at 0x105812eb8> 

EDIT: In interactive mode (not in a Jupyter laptop), HTML formatting is not displayed, and it seems that Pandas does not have a basic ascii output style for tables.

+1
source

All Articles