Python Forum

Full Version: reindex()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
import pandas as pd
import numpy as np
index = [('California', 2000), ('California', 2010),
         ('New York', 2000), ('New York', 2010),
         ('Texas', 2000), ('Texas', 2010)]
populations = [33871648, 37253956,
               18976457, 19378102,
               20851820, 25145561]
index = pd.MultiIndex.from_tuples(index)
print(index)
pop = populations.reindex(index)
Error:
AttributeError Traceback (most recent call last) in 9 index = pd.MultiIndex.from_tuples(index) 10 print(index) ---> 11 pop = populations.reindex(index) 12 #pop_df = pop.unstack() AttributeError: 'list' object has no attribute 'reindex'
I'm trying to reindex to get something like this
Output:
California 2000 33871648 2010 37253956 New York 2000 18976457 2010 19378102 Texas 2000 20851820 2010 25145561
Any idea how to solve this error? In my e-book this code works.
Just remove line No. 11 from your code and try the following instead:

what_you_want = pd.DataFrame(populations, index=index)
You tried to reindex a list instance, which doesn't have the .reindex method. Instead, you need to create a dataframe using a specific index.
Yes, as the error message showed. Strange, but I relatively often see mistakes in programming books.