Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access upper left cell of a pandas dataframe?

Here is my Python's Pandas dataframe. How can I access the upper left cell where "gender" is in and change the text? "gender" is not in names.columns. So I thought this might be names.index.name but that was not it.

import pandas as pd

names = pd.DataFrame({'births': {0: 7065, 1: 2604, 2: 2003, 3: 1939, 4: 1746},
 'gender': {0: 'F', 1: 'M', 2: 'F', 3: 'M', 4: 'F'},
 'name': {0: 'mary', 1: 'anna', 2: 'emma', 3: 'elizabeth', 4: 'minnie'},
 'year': {0: 1880, 1: 1880, 2: 1880, 3: 1880, 4: 1880}})

names = names.pivot_table(index=['name', 'year'], columns='gender', values='births').reset_index()

enter image description here

like image 296
E.K. Avatar asked Mar 18 '16 21:03

E.K.


1 Answers

gender is the name of the columns index:

In [16]:
names.columns.name = 'something'
names

Out[16]:
something       name  year     F     M
0               anna  1880   NaN  2604
1          elizabeth  1880   NaN  1939
2               emma  1880  2003   NaN
3               mary  1880  7065   NaN
4             minnie  1880  1746   NaN

You can see this when you look at the .columns object:

In [18]:
names.columns

Out[18]:
Index(['name', 'year', 'F', 'M'], dtype='object', name='gender')

I guess it does confusingly look like the index from the output

like image 196
EdChum Avatar answered Sep 21 '22 02:09

EdChum