显示的列名称与Pandas中的词典键名称不同吗? [英] Display column name different from dictionary key name in Pandas?

查看:92
本文介绍了显示的列名称与Pandas中的词典键名称不同吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Pandas的新手,看到有很多更改列标题的方法. 例如,set_axis命令的工作方式如下:

I am new to Pandas and see that there are numerous ways to change column headers. For example, the set_axis command works like this :

>>> 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

或者,可以使用

df.columns = ['A']

更改列名.

但是现在看来,如果我只想更改列标题仅用于显示目的(因为标题标签不方便用作字典键),我必须创建一个全新的数据框:

But now, it seems that if I want to change the column header for display purposes only (because the header label is inconvenient to use as a dictionary key), I have to create an entirely new data frame :

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

这是对的吗?还是我错过了什么?不得不重新创建仅用于打印的新数据帧似乎浪费了内存.我以为Pandas将有一种存储内部键"和单独的列标签的方法,该标签仅用于显示目的.

Is this right? Or am I missing something? It seems a waste of memory to have to recreate a new data frame just for printing. I would have thought that Pandas would have a way to store an internal "key" and a separate column label, used only for display purposes.

推荐答案

如果您首先设置了将短名称转换为长名称的词典:

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

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

然后,使用rename随时显示长名称确实很容易:

Then it's really easy to use rename to display the long names whenever you want:

df.rename(di,axis=1)

   long name for a
0                0
1                1
2                2

请注意,这仅适用于一列,但是一旦您设置了字典,其100列的语法就和1列一样简洁.

Note that this is just for one column, but once you set up the dictionary the syntax is just as concise for 100 columns as it is for 1.

您也不必以这种方式进行任何永久更改.只要您想以不同的方式显示内容,只需添加重命名方法即可.或者,也可以将长名称存储在永久数据帧中,然后根据需要使用字典显示短名称.

You also don't have to make any permanent changes this way. Just add the rename method whenever you want to display things differently. Or alternatively, store the long names in the permanent dataframe and just use a dictionary to display the short names as needed.

老实说,我认为这比将标签存储为列元数据要难得多,因为即使那样,您仍经常需要显式指定短名称或长名称,并且为此需要某种关键字参数.而且,由于python的词典非常灵活,因此您在这里有很多选择:您可以将短,中,长名称存储为词典,并设置功能以根据长名称自动创建短名称,等等.

Honestly, I don't think this is any harder than if the labels were stored as column metadata since even then you'd often want to specify short or long names explicitly and would need some sort of keyword argument for that. And also, because python's dictionaries are so flexible, you have tons of options here: you could have short, medium, long names stored as dictionaries, and set up functions to automatically create short names from long names, etc.

这篇关于显示的列名称与Pandas中的词典键名称不同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆