AttributeError: 'list' object has no attribute 'keys' when attempting to create DataFrame from list of dicts

14,944

Solution 1

So turns out I wasnt actually operating on a list of just dictionaries, there was a little bastard list hiding at the end there.

Sorry ya'll!

Solution 2

I can't reproduce your error with the data given, I get a KeyError.

But why even use from_records?

pd.DataFrame(Mixed_and_Poured[0]).set_index('Year')

Out:

     Country Internet users
Year                       
2000  Brazil            2.9
2005  Brazil             21
2010  Brazil           40.7
2011  Brazil             45
Share:
14,944
Vincent Buscarello
Author by

Vincent Buscarello

My name is Vincent Buscarello. I am a full-stack (primarily backend) developer always looking to do something useful for the world with my skills! I am a student of international development looking to use code to kick poverty's ass! I am interested in becoming a dev for either a non-profit or any socially minded businesses. I've only been coding since July 2015, going mostly self taught from meetups and online resources like Udacity and Pluralsight. I am open to hacking on really anything interesting, so feel free to reach out!

Updated on June 04, 2022

Comments

  • Vincent Buscarello
    Vincent Buscarello almost 2 years

    Thanks a ton for any help,

    I have a list of dictionaries that I need to put in a data frame. I know the normal method in pandas is

    final_df=pd.DataFrame.from_records(Mixed_and_Poured[0], index='year')
    

    where Mixed_and_poured is a list containing another list that actually holds the dictionaries

    print Mixed_and_Poured
    [[{'Country': 'Brazil', u'Internet users': '2.9', 'Year': '2000'}, {'Country': 'Brazil', u'Internet users': '21', 'Year': '2005'}, {'Country': 'Brazil', u'Internet users': '40.7', 'Year': '2010'}, {'Country': 'Brazil', u'Internet users': '45', 'Year': '2011'}, 
    

    I could swear

    final_df=pd.DataFrame.from_records(Mixed_and_Poured[0], index='year')
    

    was just working!! but when I ran it today it throws

    AttributeError: 'list' object has no attribute 'keys'
    

    Why is it looking for keys in this list now?