Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reindex()
#1
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.
Reply
#2
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.
Reply
#3
Yes, as the error message showed. Strange, but I relatively often see mistakes in programming books.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why reindex converts integer to decimal? new_to_python 2 2,098 Feb-08-2020, 02:27 PM
Last Post: new_to_python
  reindex dataframe after sorting Clunk_Head 4 4,723 Jun-26-2019, 01:04 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020