簡體   English   中英

TypeError:選擇行數據框熊貓的子集時需要一個整數

[英]TypeError: an integer is required when select subset of rows dataframe pandas

 {'contributors': None,
 'coordinates': None,
 'created_at': 'Tue Aug 02 19:51:58 +0000 2016',
 'entities': {'hashtags': [],
  'symbols': [],
  'urls': [],
  'user_mentions': [{'id': 873491544,
    'id_str': '873491544',
    'indices': [0, 13],
    'name': 'Kenel M',
    'screen_name': 'KxSweaters13'}]},
 'favorite_count': 1,
 'favorited': False,
 'geo': None,
 'id': 760563814450491392,
 'id_str': '760563814450491392',
 'in_reply_to_screen_name': 'KxSweaters13',
 'in_reply_to_status_id': None,
 'in_reply_to_status_id_str': None,
 'in_reply_to_user_id': 873491544,
 'in_reply_to_user_id_str': '873491544',
 'is_quote_status': False,
 'lang': 'en',
 'metadata': {'iso_language_code': 'en', 'result_type': 'recent'},
 'place': {'attributes': {},
  'bounding_box': {'coordinates': [[[-71.813501, 42.4762],
     [-71.702186, 42.4762],
     [-71.702186, 42.573956],
     [-71.813501, 42.573956]]],
   'type': 'Polygon'},
  'contained_within': [],
  'country': 'Australia',
  'country_code': 'AUS',
  'full_name': 'Melbourne, V',
  'id': 'c4f1830ea4b8caaf',
  'name': 'Melbourne',
  'place_type': 'city',
  'url': 'https://api.twitter.com/1.1/geo/id/c4f1830ea4b8caaf.json'},
 'retweet_count': 0,
 'retweeted': False,
 'source': '<a href="http://twitter.com/download/android" rel="nofollow">Twitter for Android</a>',
 'text': '@KxSweaters13 are you the kenelx13 I see owning leominster for team valor?',
 'truncated': False,
 'user': {'contributors_enabled': False,
  'created_at': 'Thu Apr 21 17:09:52 +0000 2011',
  'default_profile': False,
  'default_profile_image': False,
  'description': "Arbys when it's cold. Kimballs when it's warm. @Ally__09 all year. Comp sci classes sometimes.",
  'entities': {'description': {'urls': []}},
  'favourites_count': 1106,
  'follow_request_sent': None,
  'followers_count': 167,
  'following': None,
  'friends_count': 171,
  'geo_enabled': True,
  'has_extended_profile': False,
  'id': 285715182,
  'id_str': '285715182',
  'is_translation_enabled': False,
  'is_translator': False,
  'lang': 'en',
  'listed_count': 2,
  'location': 'MA',
  'name': 'Steve',
  'notifications': None,
  'profile_background_color': '131516',
  'profile_background_image_url': 'http://abs.twimg.com/images/themes/theme14/bg.gif',
  'profile_background_image_url_https': 'https://abs.twimg.com/images/themes/theme14/bg.gif',
  'profile_background_tile': True,
  'profile_banner_url': 'https://pbs.twimg.com/profile_banners/285715182/1462218226',
  'profile_image_url': 'http://pbs.twimg.com/profile_images/727223698332200961/bGPjGjHK_normal.jpg',
  'profile_image_url_https': 'https://pbs.twimg.com/profile_images/727223698332200961/bGPjGjHK_normal.jpg',
  'profile_link_color': '4A913C',
  'profile_sidebar_border_color': 'FFFFFF',
  'profile_sidebar_fill_color': 'EFEFEF',
  'profile_text_color': '333333',
  'profile_use_background_image': True,
  'protected': False,
  'screen_name': 'StephenBurke_',
  'statuses_count': 5913,
  'time_zone': 'Eastern Time (US & Canada)',
  'url': None,
  'utc_offset': -14400,
  'verified': False}}

我有一個包含json對象列表的json文件(每個對象都具有上述結構)

所以我將其讀入數據框:

df = pd.read_json('data.json')

然后我嘗試通過以下方式獲取所有屬於“城市”類型的行:

df = df[df['place']['place_type'] == 'city']

但是隨后我得到了'TypeError:需要一個整數'在處理上述異常期間,發生了另一個異常:KeyError:'place_type'

然后我嘗試了:

df['place'].head(3)
=>
0    {'id': '01864a8a64df9dc4', 'url': 'https://api...
1    {'id': '01864a8a64df9dc4', 'url': 'https://api...
2    {'id': '0118c71c0ed41109', 'url': 'https://api...
Name: place, dtype: object

所以df ['place']返回一個序列,其中鍵是索引,這就是為什么我得到TypeError的原因

我也嘗試選擇第一行的place_type ,它工作得很好:

df.iloc[0]['place']['place_type']
=>
city

問題是在這種情況下如何過濾出行?

解:

好的,所以問題出在以下事實: pd.read_json無法處理嵌套的JSON結構,因此我要做的是標准化json對象:

with open('data.json') as jsonfile:
    data = json.load(jsonfile)

df = pd.io.json.json_normalize(data)

df = df[df['place.place_type'] == 'city']

您可以使用列表推導來進行所需的過濾。

df = [loc for loc in df if d['place']['place_type'] == 'city']

這將為您提供一個數組,其中元素place_type'city'

我不知道您是否必須使用作為索引的place_type來顯示包含city的所有行。

“然后我嘗試通過以下方式獲取所有city類型的行:”

這樣,您可以在列place獲取包含city的所有行:

df = df[(df['place'] == 'city')]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM