Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

giving a name to a pandas dataframe?

Tags:

python

pandas

When I look to a pd df :

enter image description here

I tell myself I would like to fill the upper left empty space by a dataframe name. Is that possible ?

(Here it would be to put a text on the empty cell on top of "Lib_ze" and on left of "nunique".)

like image 988
Romain Jouin Avatar asked Jul 17 '16 21:07

Romain Jouin


People also ask

Can you name a Pandas DataFrame?

You can use the rename() method of pandas. DataFrame to change column/index name individually. Specify the original name and the new name in dict like {original name: new name} to columns / index parameter of rename() .

Can you give a title to a DataFrame?

Access the index of a DataFrame with pandas. DataFrame. index . Assign a title to the name attribute of the index.

How do you assign DataFrame column names?

To set column names of DataFrame in Pandas, use pandas. DataFrame. columns attribute. Assign required column names as a list to this attribute.


1 Answers

It is possible if set columns names:

df.columns.name = 'text'

Or rename_axis (new in pandas 0.18.0):

df = df.rename_axis('text', axis=1)

Sample:

df = df.rename_axis('text', axis=1)
print (df)
text              nunique
Lib_ze                295
cd_cmn              31870
cd_ze                 295
code_commune_zdc    31870
unite                 638

If set index name:

#df.index.name = 'text'
df = df.rename_axis('text')
print (df)
                  nunique
text                     
Lib_ze                295
cd_cmn              31870
cd_ze                 295
code_commune_zdc    31870
unite                 638
like image 170
jezrael Avatar answered Sep 20 '22 03:09

jezrael