4

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?

Vincent Buscarello
  • 395
  • 2
  • 7
  • 19
  • I am pouring over [this related question](http://stackoverflow.com/questions/23314939/converting-a-list-of-dicts-to-a-pandas-dataframe?rq=1) put cant figure out whats wrong with my solution here – Vincent Buscarello Jan 16 '16 at 19:57
  • Do you need another `[0]`? Evidently it's expecting a dictionary. – jonrsharpe Jan 16 '16 at 19:59
  • Using pandas 0.16.2 and Python 3.4, I get a good dataframe **if** I correct the `index` to `'Year'` (note capitalization) and I remove the trailing comma from `Mixed_and_Poured` and add two closing brackets `]]` instead. – MattDMo Jan 16 '16 at 20:06

3 Answers3

4

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!

Vincent Buscarello
  • 395
  • 2
  • 7
  • 19
3

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
Liam Foley
  • 7,432
  • 2
  • 26
  • 24
0

I had this issue as well when a couple of items from a list were unavailable (None). The list was quite large so I didn't notice at first. Easiest quick-fix I used was to make a new list with just the items was None:

list1 = [2,3,4, None, 2]
list1 = [item for item in list1 if item != None]
list1
[2, 3, 4, 2]
Daniel
  • 473
  • 4
  • 9
  • Ha, been some time since I looked at this! Only issue I see there is a vague try except, which is an anti-pattern. Consider catching a specific exception like an `AttributeError` or you may accidentally suppress a serious flaw in the code that will be agonizing to find later. https://stackoverflow.com/questions/14797375/should-i-always-specify-an-exception-type-in-except-statements – Vincent Buscarello Apr 17 '18 at 21:02
  • 1
    You are right, try-excepts should be more specific. A different approach would be to check if the object is None. – Daniel Apr 20 '18 at 09:46