Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将嵌套列表中的值存储到dataframe_Python_Python 3.x_Pandas_List_Dataframe - Fatal编程技术网

Python 将嵌套列表中的值存储到dataframe

Python 将嵌套列表中的值存储到dataframe,python,python-3.x,pandas,list,dataframe,Python,Python 3.x,Pandas,List,Dataframe,初始数据帧 Media_ID Post Number 0 2064531743772077759 1 1 2172781699280526966 2 2 2161918718997085520 3 3 2199646075933418046 4 4 1525499382425703584 5 输入到以下位置的媒

初始数据帧

            Media_ID            Post Number
0   2064531743772077759             1
1   2172781699280526966             2
2   2161918718997085520             3
3   2199646075933418046             4
4   1525499382425703584             5
输入到以下位置的媒体ID:
instagram。通过id获取媒体评论(2064531743772077759100)
将返回媒体id为2064531743772077759的帖子的100条评论。 循环中10项的结果如下所示:

[{'comment0': {'next_page': None,
   'comments': [<igramscraper.model.comment.Comment at 0x27c09e389e8>,
    <igramscraper.model.comment.Comment at 0x27c09e38630>,
    <igramscraper.model.comment.Comment at 0x27c09e38f98>,
    <igramscraper.model.comment.Comment at 0x27c09eba0f0>]}},
 {'comment1': {'next_page': None,
   'comments': [<igramscraper.model.comment.Comment at 0x27c09eba278>,
    <igramscraper.model.comment.Comment at 0x27c09eba1d0>,
    <igramscraper.model.comment.Comment at 0x27c09eba160>,
...
{'comment9': {'next_page': None,
   'comments': [<igramscraper.model.comment.Comment at 0x27c0a1ba7b8>,
    <igramscraper.model.comment.Comment at 0x27c0a1ba860>,
    <igramscraper.model.comment.Comment at 0x27c0a1ba5c0>,
    <igramscraper.model.comment.Comment at 0x27c0a1ba630>,
    <igramscraper.model.comment.Comment at 0x27c0a1f2860>]}}]
预期产出:

请参见修订后的问题。我只需要评论0,评论1。。。从列表中删除,并将每一行作为单独的变量@deadshot,但commecnts是对象列表?我不确定如何检查?如果您使用上述数据创建dataframe,您将获得每一行的注释对象列表。以前,我没有使用循环。因此,我做了以下操作:
comment0=instagram.get\u media\u comment\u by\u id(code,100)
comment0
是一个DICT。然后,我使用枚举代码创建一个数据帧。现在我尝试使用循环,但是
comment0
卡在列表中。请查看修订后的问题。我只需要评论0,评论1。。。从列表中删除,并将每一行作为单独的变量@deadshot,但commecnts是对象列表?我不确定如何检查?如果您使用上述数据创建dataframe,您将获得每一行的注释对象列表。以前,我没有使用循环。因此,我做了以下操作:
comment0=instagram.get\u media\u comment\u by\u id(code,100)
comment0
是一个DICT。然后,我使用枚举代码创建一个数据帧。现在我尝试使用循环,但是
comment0
卡在列表中。
for i,_ in enumerate(df['link'], 1):
    df2pi = pd.DataFrame( [{'Comments':comment.text} for comment in locals()[f"comment{i}"]['comments'] ] )
    df2pi['Date']=df['date'][i-1]