Formatting df's multi index
I have a multi-index dataframe used in a block of code. It's index looks like this:
MultiIndex([('American Indian or Alaska Native', '1-4 years'),
('American Indian or Alaska Native', '10-14 years'),
('American Indian or Alaska Native', '15-17 years'),
('American Indian or Alaska Native', '18-19 years'),
('American Indian or Alaska Native', '20-24 years'),
('American Indian or Alaska Native', '25-29 years'),
('American Indian or Alaska Native', '30-34 years'),
('American Indian or Alaska Native', '35-39 years'),
('American Indian or Alaska Native', '40-44 years'),
('American Indian or Alaska Native', '45-49 years'),
...
( 'White', '55-59 years'),
( 'White', '60-64 years'),
( 'White', '65-69 years'),
( 'White', '70-74 years'),
( 'White', '75-79 years'),
( 'White', '80-84 years'),
( 'White', '85+ years'),
( 'White', '1 year'),
( 'COUNTY_LABEL', ''),
( 'RaceGroups_Total', '')],
names=['Race', 'Age'], length=142)
but I need to it be in a different format to work with the block of code that I have. I need it to be formatted like this:
MultiIndex([( 'Race', 'Age'),
('American Indian or Alaska Native', '1-4 years'),
('American Indian or Alaska Native', '10-14 years'),
('American Indian or Alaska Native', '15-17 years'),
('American Indian or Alaska Native', '18-19 years'),
('American Indian or Alaska Native', '20-24 years'),
('American Indian or Alaska Native', '25-29 years'),
('American Indian or Alaska Native', '30-34 years'),
('American Indian or Alaska Native', '35-39 years'),
('American Indian or Alaska Native', '40-44 years'),
...
( 'White', '50-54 years'),
( 'White', '55-59 years'),
( 'White', '60-64 years'),
( 'White', '65-69 years'),
( 'White', '70-74 years'),
( 'White', '75-79 years'),
( 'White', '80-84 years'),
( 'White', '85+ years'),
( 'White', '1 year'),
( 'RaceGroups_Total', 'Unnamed: 141_level_1')],
I am new to Pandas and very new to multi-indexing. Right now the solution I have is to export the df to a csv and then read it in as:
r3=pd.read_csv(rC:\Users\myusername\Desktop\r3.csv, header = [0,1])
but seems like a gimmicky solution. Is there an easy way to reformat this df? Thanks!
Category Data Science