通过过滤带有嵌套字典的字典来创建熊猫数据框

时间:2019-10-22 04:23:21

标签: python json python-3.x dataframe tweepy

我正在尝试使用仅字典中的几列来创建熊猫数据框。字典只是twitter API中User对象的._json。

通过遍历每个用户并删除每个不需要的密钥,我已经达到了想要的目的,但是这太昂贵了。为了避免这种情况,我尝试使用:


data = 
[{'id': 1085741390527692800,
  'status': {'created_at': 'Tue Aug 06 01:56:58 +0000 2019',
             'entities': {'hashtags': []},
             'id': 1158557544052379649,
             'id_str': '1158557544052379649',
             'text': '@EnCicla Se tiene alguna fecha prevista para activar la '
                     'estación de Parques del Rio?',
             'truncated': False}}]

这只是用户对象中两个键的一个示例(我从嵌套字典中删除了一些键)。我想做的是:

df = pd.DataFrame(columns = cols)

for column in df.columns:
    df[column] = list(map(lambda obj: obj[column]), data)

但是我得到了错误:

TypeError:字符串索引必须为整数

我也尝试过使用pandas.io.json_normalize,然后过滤创建的数据框,但这也太昂贵了。

编辑:

好的,很抱歉犯错了。我只给你整个字典:

data = {'id': 1085741390527692800,
 'id_str': '1085741390527692800',
 'name': 'Ramon',
 'screen_name': 'Ramon15862280',
 'location': '',
 'profile_location': None,
 'description': '',
 'url': None,
 'entities': {'description': {'urls': []}},
 'protected': False,
 'followers_count': 0,
 'friends_count': 5,
 'listed_count': 0,
 'created_at': 'Thu Jan 17 03:31:34 +0000 2019',
 'favourites_count': 0,
 'utc_offset': None,
 'time_zone': None,
 'geo_enabled': False,
 'verified': False,
 'statuses_count': 3,
 'lang': None,
 'status': {'created_at': 'Tue Aug 06 01:56:58 +0000 2019',
  'id': 1158557544052379649,
  'id_str': '1158557544052379649',
  'text': '@EnCicla Se tiene alguna fecha prevista para activar la estación de Parques del Rio?',
  'truncated': False,
  'entities': {'hashtags': [],
   'symbols': [],
   'user_mentions': [{'screen_name': 'EnCicla',
     'name': 'EnCicla',
     'id': 387985142,
     'id_str': '387985142',
     'indices': [0, 8]}],
   'urls': []},
  'source': '<a href="https://mobile.twitter.com" rel="nofollow">Twitter Web App</a>',
  'in_reply_to_status_id': 1158425030986031104,
  'in_reply_to_status_id_str': '1158425030986031104',
  'in_reply_to_user_id': 387985142,
  'in_reply_to_user_id_str': '387985142',
  'in_reply_to_screen_name': 'EnCicla',
  'geo': None,
  'coordinates': None,
  'place': None,
  'contributors': None,
  'is_quote_status': False,
  'retweet_count': 0,
  'favorite_count': 2,
  'favorited': False,
  'retweeted': False,
  'lang': 'es'},
 'contributors_enabled': False,
 'is_translator': False,
 'is_translation_enabled': False,
 'profile_background_color': 'F5F8FA',
 'profile_background_image_url': None,
 'profile_background_image_url_https': None,
 'profile_background_tile': False,
 'profile_image_url': 'http://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png',
 'profile_image_url_https': 'https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png',
 'profile_link_color': '1DA1F2',
 'profile_sidebar_border_color': 'C0DEED',
 'profile_sidebar_fill_color': 'DDEEF6',
 'profile_text_color': '333333',
 'profile_use_background_image': True,
 'has_extended_profile': False,
 'default_profile': True,
 'default_profile_image': True,
 'following': False,
 'follow_request_sent': False,
 'notifications': False,
 'translator_type': 'none',
 'suspended': False,
 'needs_phone_verification': False}

例如,我想过滤键:“ id”和“ status”。

0 个答案:

没有答案