如何从Instagram JSON数据中获取Pandas DataFrame

发布于 2025-02-09 19:55:55 字数 2358 浏览 2 评论 0原文

我对所有这一切都是很新的,我花了一段时间就花了一段时间的Python Bootcamp,现在正在努力将一些Instagram数据纳入我了解的格式。

使用以下代码:

# Importing packages
import json
import re
import collections
import pandas as pd
import datetime as dt
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

# Loading downloaded instagram data
json_data = {}
data_path = "C:/Users/etc.json"
with open(data_path) as file:
    json_data = json.load(file)

print(json_data)

我将获得以下看起来很有希望的输出:

{'relationships_followers': [{'title': '', 'media_list_data': [], 'string_list_data': [{'href': 'https://www.instagram.com/username1', 'value': 'username1', 'timestamp': 1655411505}]}, {'title': '', 'media_list_data': [], 'string_list_data': [{'href': 'https://www.instagram.com/username2', 'value': 'username2', 'timestamp': 1655149264}]}, {'title': '', 'media_list_data': [], 'string_list_data': [{'href': 'https://www.instagram.com/username3', 'value': 'username3', 'timestamp': 1655129904}]}, etc.....

type = dict,

但是当我尝试将其转换为熊猫数据框架时,它会出现奇怪的

dfp = pd.read_json(data_path, orient = 'records')
print(dfp)
print(type(dfp))

输出:

                               relationships_followers
0    {'title': '', 'media_list_data': [], 'string_l...
1    {'title': '', 'media_list_data': [], 'string_l...
2    {'title': '', 'media_list_data': [], 'string_l...
3    {'title': '', 'media_list_data': [], 'string_l...
4    {'title': '', 'media_list_data': [], 'string_l...
..                                                 ...
575  {'title': '', 'media_list_data': [], 'string_l...
576  {'title': '', 'media_list_data': [], 'string_l...
577  {'title': '', 'media_list_data': [], 'string_l...
578  {'title': '', 'media_list_data': [], 'string_l...
579  {'title': '', 'media_list_data': [], 'string_l...

[580 rows x 1 columns]
<class 'pandas.core.frame.DataFrame'>

如何停止将“ Realenth_followers”作为一个孤独的列?

试图获得如下输出:

         href             value          timestamp
0        www.inst...      username1      DDMMYY
1        www.inst...      username2      DDMMYY
2        www.inst...      username3      DDMMYY
3        www.inst...      username4      DDMMYY
...
578      www.inst...      username578    DDMMYY
579      www.inst...      username579    DDMMYY

I am quite new to all this, I took a short Python bootcamp a while back and am now struggling to get some Instagram data into a format I understand.

Using the following code:

# Importing packages
import json
import re
import collections
import pandas as pd
import datetime as dt
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

# Loading downloaded instagram data
json_data = {}
data_path = "C:/Users/etc.json"
with open(data_path) as file:
    json_data = json.load(file)

print(json_data)

I get the following output which looks promising:

{'relationships_followers': [{'title': '', 'media_list_data': [], 'string_list_data': [{'href': 'https://www.instagram.com/username1', 'value': 'username1', 'timestamp': 1655411505}]}, {'title': '', 'media_list_data': [], 'string_list_data': [{'href': 'https://www.instagram.com/username2', 'value': 'username2', 'timestamp': 1655149264}]}, {'title': '', 'media_list_data': [], 'string_list_data': [{'href': 'https://www.instagram.com/username3', 'value': 'username3', 'timestamp': 1655129904}]}, etc.....

type = dict

But when I try to convert it into a pandas dataframe it presents strangely

dfp = pd.read_json(data_path, orient = 'records')
print(dfp)
print(type(dfp))

Output:

                               relationships_followers
0    {'title': '', 'media_list_data': [], 'string_l...
1    {'title': '', 'media_list_data': [], 'string_l...
2    {'title': '', 'media_list_data': [], 'string_l...
3    {'title': '', 'media_list_data': [], 'string_l...
4    {'title': '', 'media_list_data': [], 'string_l...
..                                                 ...
575  {'title': '', 'media_list_data': [], 'string_l...
576  {'title': '', 'media_list_data': [], 'string_l...
577  {'title': '', 'media_list_data': [], 'string_l...
578  {'title': '', 'media_list_data': [], 'string_l...
579  {'title': '', 'media_list_data': [], 'string_l...

[580 rows x 1 columns]
<class 'pandas.core.frame.DataFrame'>

How do I stop taking "relationships_followers" as a lonely column?

Trying to get an output like the below:

         href             value          timestamp
0        www.inst...      username1      DDMMYY
1        www.inst...      username2      DDMMYY
2        www.inst...      username3      DDMMYY
3        www.inst...      username4      DDMMYY
...
578      www.inst...      username578    DDMMYY
579      www.inst...      username579    DDMMYY

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌吟 2025-02-16 19:55:55

尝试对您的大师命令这样做。

worthy_data = json_data.get('relationship_followers')

wanted_dicts = [k:v for (k,v) in worthy_data.items() if k == 'string_list_data']

pd.DataFrame(wanted_dicts)

Try doing this to your master dict.

worthy_data = json_data.get('relationship_followers')

wanted_dicts = [k:v for (k,v) in worthy_data.items() if k == 'string_list_data']

pd.DataFrame(wanted_dicts)
白云不回头 2025-02-16 19:55:55

在这种情况下,您可以使用 pd.sson_normalize(to)提取HREFvaluetimestamp列从string_list_data dictionary中提取。

pd.json_normalize(json_data['relationships_followers'], 'string_list_data')

# Output :
#                                   href      value   timestamp
# 0  https://www.instagram.com/username1  username1  1655411505
# 1  https://www.instagram.com/username2  username2  1655149264
# 2  https://www.instagram.com/username3  username3  1655129904

In this case you can use pd.json_normalize() to extract the href, value, timestamp columns from the string_list_data dictionary.

pd.json_normalize(json_data['relationships_followers'], 'string_list_data')

# Output :
#                                   href      value   timestamp
# 0  https://www.instagram.com/username1  username1  1655411505
# 1  https://www.instagram.com/username2  username2  1655149264
# 2  https://www.instagram.com/username3  username3  1655129904
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文