Reputation: 81
index = {'California': 2000, 'California': 2010, 'New York': 2000, 'New York': 2010, 'Texas': 2000, 'Texas': 2010}
population=[23434,56566,76766,546546,45345,56546
]
Any method to create data frame in python(pandas) like this:
state Year population
California 2000 23434
2010 56566
New York 2000 76766
2010 546546
Texas 2000 45345
2010 56546
Please write complete code to create dataframe in python
Upvotes: 0
Views: 77
Reputation: 863166
Your index is broken, because dictionary has always unique keys in python.
For MultiIndex
is possible use MultiIndex.from_product
, but length of MultiIndex
has to be same like length of population
list:
mux = pd.MultiIndex.from_product([['California','New York','Texas'],
[2000, 2010]], names=['state', 'year'])
population=[23434,56566,76766,546546,45345,56546]
df = pd.DataFrame({'population':population}, index=mux)
print (df)
population
state year
California 2000 23434
2010 56566
New York 2000 76766
2010 546546
Texas 2000 45345
2010 56546
print (len(mux))
6
print (len(population))
6
Upvotes: 3