私はmongodbからデータを抽出しようとしていました。だから、私はデータフレームとしてパンダを使っていました。私はtwitterデータセットを使用していました。データセットは、JSONにあったと私はそれをデータベースにインポートするとき、このように見えます:pandaデータフレームから親の子ノードを表示するにはどうすればいいですか?
user:Object
id:1292598776
id_str:1292598776
name:ahmd
screen_name:sameh7753
location:
url:null
description:null
protected:false
followers_count:5
friends_count:76
listed_count:0
created_at:Sat Mar 23 21:59:37 +0000 2013
favourites_count:1
utc_offset:null
time_zone:null
geo_enabled:true
lang:ar
contributors_enabled:false
is_translator:false
profile_background_color:C0DEED
profile_use_background_image:true
default_profile:true
default_profile_image:false
follow_request_sent:null
だから、ここ「ユーザーが」親で、その下に多くのchildren.Thereがあるが、データセットにあまりにも他のフィールドです。 だから、私は2013年にツイートされたツイートを見つけるためのクエリを実行しようとしていたし、ツイートの場所は"US"
です。そして、私はそれらのカーソルをパンダデータフレームに格納していました。だから私がデータフレームを印刷していたときに、私はそれらがscreen_name
であることを期待していたが、印刷されず、これらのデータにアクセスできなかった。ここで
import pandas as pd
from pymongo import MongoClient
import matplotlib.pyplot as plt
import re
pd.set_option('display.expand_frame_repr', False)
def _connect_mongo(host, port, db):
conn = MongoClient(host, port)
return conn[db]
def read_mongo(db, collection, host, port):
""" Read from Mongo and Store into DataFrame """
# Connect to MongoDB
db = _connect_mongo(host=host, port=port, db=db)
cursor = db[collection].find({'created_at':{'$regex': '2013'}},
{'place.country':'US'}, no_cursor_timeout=True).toArray()
print cursor
# Expand the cursor and construct the DataFrame
df = pd.DataFrame(list(cursor))
return df
db = 'twittersmall' #'twitter'
collection='twitterdata' #'twitterCol' #
#query={'lang':'{$exists: true}'}
host='localhost'
port=27017
var = read_mongo(db, collection, host, port)
print var
それだけパンダのデータフレーム内user
列の下に印刷するこの:
False {u'follow_request_sent':
u'profile_use_b...
と属性の休息プリントを得ることはありませんし、私は、var['user.screen_name']
をPythonコードに書いてアクセスすることさえできません。 データにアクセスするにはどうすればよいですか?