2014-04-02 1 views
1

파이썬 편집기에서 JSON 파일을로드하여 일부 데이터를 분석 할 수있는 몇 가지 문제가 있습니다.팬더를 사용하여 Python 분석을 위해 JSON 파일 읽기

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 
} 

나는 다음을 사용하고 있습니다 : (거기에 수백이있다, 이것은 단지 하나 개의 기록이다) 'C:\Users\Admin\JSON files\file1.JSON'

그것은 다음과 같은 트윗 데이터를 포함 나는이 프로그램을 실행할 때

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'필드를 사용하면 false/trues의 개수를 계산할 수 있습니까? 주어진? JSON 파일 내의 IE, 나는 retweeted 된 트윗의 수와 그렇지 않은 수를 원한다. 사전에

덕분에, 그래 나는이에 아주 새로운 해요 ..

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을 보여주는 오는 방법()?

+0

df.info()는 값이 맞아야한다고 가정 할 때 null이 아닌 객체로 열을 표시합니다. 그것들을 객체가 아닌 값으로 변경하려면 어떻게해야합니까?' Int64Index 2 개 항목 0~1 데이터 열 (총 2 열) retweet_count 1 비 - 널 객체 USER_ID 1 비 - 널 객체 dtypes : 물체 (2)' – user1745447

+0

가 '데이터 유형이란 z'? – myacobucci

+0

하단의 @myacobucci – user1745447

답변

0

d.retweet_countretweet_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) 
+0

[ "retweet_count", "user_id", "is_retweet"]에 k가 있으면 z.iteritems()에서 k에 대해 다음 줄'd = pa.DataFrame.from_dict ([{k : v} ])'그것은 내 값 열을 객체로 변환하여 합/평균/등을 실행할 수 없었습니다. 샘플 크기를 초 단위로 시도합니다. – user1745447

+0

'print d.retweet_count'를 실행하면 정확한 출력은 무엇입니까? – myacobucci

+0

이제 문제가 해결되었습니다. 모든 문제가 아니라 JSON 데이터의 샘플을 사용하는 데 문제가 있습니다. 이전과 마찬가지로'z = pa.read_json ('C : \ Users \ Admin \ JSON files \ file1.JSON')'을 사용하고 있지만 retweet_count의 100/200 샘플을 사용하여 mean/max/diff 크기의 샘플 등. 나는 keys = z.retweet_count.keys()를 시도했다.sample_keys = keys [: 200]', 그리고'sample_keys.mean'으로 mean을 호출했지만, 작동하지 않는다. @myacobucci 덕분에 – user1745447