簡體   English   中英

導入 JSON 文件進行 Python 分析

[英]Importing JSON file for Python analysis

我正在嘗試導入一個 JSON 文件以在 Python 編輯器中使用,以便我可以對數據執行分析。 我對 Python 很陌生,所以不確定我是如何實現這一目標的。 我的 JSON 文件充滿了推文數據,示例如下:

{"id":441999105775382528,"score":0.0,"text":"blablabla","user_id":1441694053,"created":"Fri Mar 07 18:09:33 GMT 2014","retweet_id":0,"source":"<a href=\"http://twitterfeed.com\" rel=\"nofollow\">twitterfeed</a>","geo_long":null,"geo_lat":null,"location":"","screen_name":"SevenPS4","name":"Playstation News","lang":"en","timezone":"Amsterdam","user_created":"2013-05-19","followers":463,"hashtags":"","mentions":"","following":1062,"urls":"http://bit.ly/1lcbBW6","media_urls":"","favourites_count":4514,"reply_status_id":0,"reply_user_id":0,"is_truncated":false,"is_retweet":false,"original_text":null,"status_count":4514,"description":"Tweeting the latest Playstation news!","url":null,"utc_offset":3600}

我的問題:

如何導入 JSON 文件以便我可以在 Python 編輯器中對其執行分析?

如何僅對一組數據進行分析(IE 100/200 而不是所有數據)?

有沒有辦法擺脫某些字段,例如scoreuser_idcreated等,而無需手動瀏覽我的所有數據?

一些推文中包含無效/不可用的符號,是否有辦法在無需手動處理的情況下擺脫這些符號?

我會使用Pandas來完成這項工作,因為您不僅會加載 json,還會對其執行一些數據分析任務。 根據您的 json 文件的大小,這個應該這樣做:

import pandas as pd
import json

# read a sample json-file (replace the link with your file location
j = json.loads("yourfilename")
# you might select the relevant keys before constructing the data-frame
df = pd.DataFrame.from_dict([{k:v} for k,v in j.iteritems() if k in ["id","retweet_count"]])
# select a subset (the first five rows)
df.iloc[:5]
# do some analysis
df.retweet_count.sum()
>>> 200

暫無
暫無

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

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