迭代列表和Dictionary并转换为DataFrame

时间:2017-12-13 15:16:26

标签: python json pandas dictionary dataframe

我正在尝试从这些数据中获取特定列表,我将其转换为Pandas Dataframe但是我收到以下错误:

TypeError:list indices必须是整数或切片,而不是str

这是数据;

r = requests.get(url).json()

print(r)

输出:

[{'category': {'id': 34,
   'name': 'Tech',
   'shortname': 'tech',
   'sort_name': 'Tech'},
  'city': 'Edinburgh',
  'country': 'GB',
  'created': 1450173286000,
  'description': "<p>We're passionate about security, as are you.</p>\n<p>We want to invite the security community in Scotland to engage 5 or 6 times a year to discuss all things security. A informal forum to share ideas, make contacts, encourage debate.</p>\n<p>Our MeetUps will 100% NOT be sales-led. There will be no vendors, no sponsors, no obligation to talk to anyone, nor cost any money to attend.</p>\n<p>They will be hosted at a number of venues, but there will be no hosting-company focus, we merely organise and host the events, with a choice of speakers as well as the obligatory refreshments!</p>",
  'id': 19213863,
  'join_mode': 'open',
  'key_photo': {'base_url': 'https://secure.meetupstatic.com',
   'highres_link': 'https://secure.meetupstatic.com/photos/event/d/e/c/4/highres_445137028.jpeg',
   'id': 445137028,
   'photo_link': 'https://secure.meetupstatic.com/photos/event/d/e/c/4/600_445137028.jpeg',
   'thumb_link': 'https://secure.meetupstatic.com/photos/event/d/e/c/4/thumb_445137028.jpeg',
   'type': 'event'},
  'lat': 55.94,
  'link': 'https://www.meetup.com/Security-MeetUp-Scotland/',
  'localized_country_name': 'United Kingdom',
  'localized_location': 'Edinburgh, United Kingdom',
  'lon': -3.2,
  'members': 1059,
  'meta_category': {'category_ids': [34],
   'id': 292,
   'name': 'Tech',
   'photo': {'base_url': 'https://secure.meetupstatic.com',
    'highres_link': 'https://secure.meetupstatic.com/photos/event/2/e/a/d/highres_450131949.jpeg',
    'id': 450131949,
    'photo_link': 'https://secure.meetupstatic.com/photos/event/2/e/a/d/600_450131949.jpeg',
    'thumb_link': 'https://secure.meetupstatic.com/photos/event/2/e/a/d/thumb_450131949.jpeg',
    'type': 'event'},
   'shortname': 'tech',
   'sort_name': 'Tech'},
  'name': 'Security MeetUp Scotland',
  'next_event': {'id': '245752465',
   'name': 'Security Scotland Chapter 10 - hosted by Skyscanner!',
   'time': 1516820400000,
   'utc_offset': 0,
   'yes_rsvp_count': 130},
  'organizer': {'bio': 'I do Security stuff. Currently at Capital One.\nOrganiser of Security Scotland: https://www.meetup.com/Security-MeetUp-Scotland\nHusband, proud daddy, guitarist, drummer, muso, ex-DJ/producer, Fleetwood Mac aficionado, Scottish Leeds fan.',
   'id': 192768669,
   'name': 'Stu Hirst',
   'photo': {'base_url': 'https://secure.meetupstatic.com',
    'highres_link': 'https://secure.meetupstatic.com/photos/member/8/d/9/7/highres_250956247.jpeg',
    'id': 250956247,
    'photo_link': 'https://secure.meetupstatic.com/photos/member/8/d/9/7/member_250956247.jpeg',
    'thumb_link': 'https://secure.meetupstatic.com/photos/member/8/d/9/7/thumb_250956247.jpeg',
    'type': 'member'}},
  'score': 1.0,
  'state': 'U8',
  'status': 'active',
  'timezone': 'Europe/London',
  'urlname': 'Security-MeetUp-Scotland',
  'visibility': 'public',
  'who': 'Scot Security Folks'},
{'category': {'id': 34, ...
 ]

据我所知,这里有很多词典,我想得到主要的词典。我已经尝试过这样;

for item in r['category']:
    print (item['name'])
    print (item['city'])
    print (item['members'])

for item in r['meta_category']:
print (item['name'])
print (item['country'])
print (item['status'])

还有更多,但那时我收到了错误。您是否可以帮助我创建一个'name', 'city, 'country', 'lat', 'lon', 'description', 'members', 'status', 'url-name' 'category''meta_category'

<li *ngFor="let task of tasks"> <a routerLink="/task/{{task.id}}" ng-click="reloadRoute()"> {{task.name}}</a> </li> 的数据框架

1 个答案:

答案 0 :(得分:0)

所以json,你已经知道的是一个字典列表,因此,也可以有“子词典”。在你的情况下,你有一个包含很多键的大词典,还有一些较小的词典,例如'category'

类别有4个键(id,name,shortname,sort_name)

当你试图遍历r ['category']时,你发现了一个错误,因为你想在类别字典中找到的键不在那里!但在大的(在我的情况下称为数据)

所以以下工作正如人们期望的那样:

import json

data = json.load(open('test.json'))
columns = ['city', 'country', 'lat', 'lon',
           'description', 'members', 'status', 'urlname']

for meetup in data:
    print(meetup['category']['name'])
    for item in columns:
        print(meetup[item])

现在你只需要弄清楚如何将数据传输到df,这可以通过csv来完成。如果不清楚我也可以帮助你