I have a DataFrame using pandas and column labels that I need to edit to replace the original column labels.
I\'d like to change the column names in a DataFrame
I would like to explain a bit what happens behind the scenes.
Dataframes are a set of Series.
Series in turn are an extension of a numpy.array
numpy.array
s have a property .name
This is the name of the series. It is seldom that pandas respects this attribute, but it lingers in places and can be used to hack some pandas behaviors.
A lot of answers here talks about the df.columns
attribute being a list
when in fact it is a Series
. This means it has a .name
attribute.
This is what happens if you decide to fill in the name of the columns Series
:
df.columns = ['column_one', 'column_two']
df.columns.names = ['name of the list of columns']
df.index.names = ['name of the index']
name of the list of columns column_one column_two
name of the index
0 4 1
1 5 2
2 6 3
Note that the name of the index always comes one column lower.
The .name
attribute lingers on sometimes. If you set df.columns = ['one', 'two']
then the df.one.name
will be 'one'
.
If you set df.one.name = 'three'
then df.columns
will still give you ['one', 'two']
, and df.one.name
will give you 'three'
pd.DataFrame(df.one)
will return
three
0 1
1 2
2 3
Because pandas reuses the .name
of the already defined Series
.
Pandas has ways of doing multi layered column names. There is not so much magic involved but I wanted to cover this in my answer too since I don't see anyone picking up on this here.
|one |
|one |two |
0 | 4 | 1 |
1 | 5 | 2 |
2 | 6 | 3 |
This is easily achievable by setting columns to lists, like this:
df.columns = [['one', 'one'], ['one', 'two']]