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()
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With