【发布时间】:2020-10-18 22:39:21
【问题描述】:
我正在尝试将 JSON 文件中的数据访问到 pandas 数据帧中,并且似乎被困在如何检索 JSON 映射中的数据。
我想将此json的用户对象中的followers_count实体检索到数据框中。
JSON 文件(示例记录)如下:
{"created_at": "Tue Aug 01 16:23:56 +0000 2017", "id": 892420643555336193, "retweet_count": 12345, "favorite_count": 23456, "user": {"id": 4196983835, "followers_count": 3200889, "friends_count": 104}}
这是我在代码方面的内容(不起作用,因为我不知道如何获取用户对象中的 follower_count :
tweet_data_df = pd.read_json('tweet-json.txt', lines=True)
#Doesnt work
#tweet_data_df = tweet_data_df[['id', 'favorite_count', 'retweet_count', 'created_at', 'user''followers_count']]
#works but not enough for me
tweet_data_df = tweet_data_df[['id', 'favorite_count', 'retweet_count', 'created_at']]
tweet_data_df.head(5)
感谢您的帮助!
【问题讨论】:
-
如果 json 字典的 depth = 2 你可以使用
pd.DataFrame(json_dict).apply(pd.Series)?
标签: python json pandas dataframe