westonplatter
westonplatter

Reputation: 1495

What is the 'name' in pandas.DataFrame.columns?

When I execute a pivot on a pandas dataframe,

df = pd.DataFrame({'foo': ['one', 'one', 'one', 'two', 'two','two'],
    'bar': ['A', 'B', 'C', 'A', 'B', 'C'],
    'baz': [1, 2, 3, 4, 5, 6],
    'zoo': ['x', 'y', 'z', 'q', 'w', 't']})

df.pivot(index='foo', columns='bar', values='baz')


>>> bar A   B   C
    foo         
    one 1   2   3
    two 4   5   6

Which has these columns,

df.pivot(index='foo', columns='bar', values='baz').columns

>>> Index(['A', 'B', 'C'], dtype='object', name='bar')

My question is, what does name=bar part mean?

Upvotes: 0

Views: 290

Answers (1)

sacuL
sacuL

Reputation: 51335

From the docs

name : object

Name to be stored in the index

In your example, it's the name of the pandas.Index that is used as the column name.

The name attribute becomes useful in some cases, for instance if you have a multiindex, you can refer to the level of the index by it's name:

>>> df
idx1  1  2  3 # <- column header 1
idx2  a  b  c # <- column header 2
vals  5  4  6

>>> df.columns
MultiIndex(levels=[[1, 2, 3], ['a', 'b', 'c']],
           labels=[[0, 1, 2], [0, 1, 2]],
           names=['idx1', 'idx2'])

>>> df.columns.get_level_values('idx1')
Int64Index([1, 2, 3], dtype='int64', name='idx1')
>>> df.columns.get_level_values('idx2')
Index(['a', 'b', 'c'], dtype='object', name='idx2')

Upvotes: 1

Related Questions