簡體   English   中英

使用pandas讀取JSON文件進行Python分析

[英]Using pandas to read JSON file for Python analysis

我遇到了一些問題,試圖在我的 Python 編輯器中加載一個 JSON 文件,以便我可以對其中的數據進行一些分析。

JSON 文件位於以下文件夾中: 'C:\\Users\\Admin\\JSON files\\file1.JSON'

它包含以下推文數據(這只是一條記錄,其中有數百條記錄):

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

我正在使用以下代碼來嘗試測試此數據:

import json
import pandas as pa
z = pa.read_json('C:\Users\Admin\JSON files\file1.JSON')
d = pa.DataFrame.from_dict([{k:v} for k,v in z.iteritems() if k in ["retweet_count", "user_id", "is_retweet"]])
print d.retweet_count.sum()

當我運行它時,它成功讀取了 JSON 文件,然后像這樣打印出 retweet_count 的列表:

0, 4514 1, 300 2, 450 3, 139等等

我的問題:我如何實際總結所有的 retweet_count/user_id 值,而不是像上面顯示的那樣只列出它們?

然后我如何將這個總和除以條目數以獲得平均值?

如何選擇 JSON 數據的樣本大小而不是全部使用? (我以為是 d.iloc[:10] 但這不起作用)

使用 JSON 文件中的“is_retweet”字段,是否可以計算給出的假/真數量? IE 在 JSON 文件中,我想要被轉發的推文數量和未被轉發的數量。

在此先感謝,是的,我對此很陌生..

z.info()給出:

<class 'pandas.core.frame.DataFrame'> Int64Index: 506 entries, 0 to 505 Data columns (total 31 columns): created 506 non-null object description 506 non-null object favourites_count 506 non-null int64 followers 506 non-null int64 following 506 non-null int64 geo_lat 10 non-null float64 geo_long 10 non-null float64 hashtags 506 non-null object id 506 non-null int64 is_retweet 506 non-null bool is_truncated 506 non-null bool lang 506 non-null object location 506 non-null object media_urls 506 non-null object mentions 506 non-null object name 506 non-null object original_text 172 non-null object reply_status_id 506 non-null int64 reply_user_id 506 non-null int64 retweet_id 506 non-null int64 retweet_count 506 non_null int64 score 506 non-null int64 screen_name 506 non-null object source 506 non-null object status_count 506 non-null int64 text 506 non-null object timezone 415 non-null object url 273 non-null object urls 506 non-null object user_created 506 non-null object user_id 506 non-null int64 utc_offset 506 non-null int64 dtypes: bool(2), float64(2), int64(11), object(16)

當我運行 d.info() 時,它為什么將 retweet_count 和 user_id 顯示為對象?

d.retweet_count是你的retweet_counts的字典列表嗎?

所以要得到總和:

keys = d.retweet_count.keys()
sum = 0
for items in keys:
    sum+=d.retweet_count[items]

要獲得平均值:

avg = sum/len(keys)

現在要獲得樣本大小,只需將keys分開:

sample_keys = keys[0:10]

得到平均值

for items in sample_keys:
     sum+=d.retweet_count[items]
avg = sum/len(sample_keys)

暫無
暫無

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

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