Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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中将嵌套列表获取到数据帧中_Python_Python 3.x_Nested Lists - Fatal编程技术网

在Python中将嵌套列表获取到数据帧中

在Python中将嵌套列表获取到数据帧中,python,python-3.x,nested-lists,Python,Python 3.x,Nested Lists,在将嵌套列表转换为Pandas数据帧时,我看到了一些其他类似的问题,但我的问题似乎有点复杂。我现在有一个列表,里面有很多鸟巢(我说的对吗?哈哈) 例如: [{'date': 'yyyy-mm-dd', 'total_comments':1, 'id': 123456, 'engagements_by_type': {'url clicks': 111, 'other clicks':222}, 'url': 'https://hi.com/stackove

在将嵌套列表转换为Pandas数据帧时,我看到了一些其他类似的问题,但我的问题似乎有点复杂。我现在有一个列表,里面有很多鸟巢(我说的对吗?哈哈)

例如:

    [{'date': 'yyyy-mm-dd',
    'total_comments':1,
    'id': 123456,
    'engagements_by_type': {'url clicks': 111, 'other clicks':222},
    'url': 'https://hi.com/stackoverflow/is/the/best',
    'posts_by_paid_unpaid': {'paid': 1, 'total': 100, 'unpaid': 99}
    'organic_impressions': ,
    'social_media_impressions': {'facebook': 2, 'twitter': 4, 'instagram': 4, 'twitch': 6,
    'total_social_media-impressions' : 10}
    {'date':....
    ......}]
*请注意,“total_social_media_impressions”是它前面嵌套列表“social_media_impressions”的总和。这使得它非常棘手

……等等。有比我提到的更多的专栏,但我只是想展示一个简短的例子

有人知道如何将这种长嵌套列表转换为数据帧吗

更新: 我使用for循环来标识列表中嵌套的列:

df = pandas.DataFrame(data)
columns = df.columns

for i in columns:
    if str(df[i][0]).startswith('{'):
        print('True')
    else:
        print('False')

下一步是找出如何正确地操作它们,使它们作为普通列出现在数据帧中,而不是嵌套在数据帧中。

我发布的解决方案假设您也希望将嵌套的dict键转换为列

import pandas as pd

data = [
            {'date': 'yyyy-mm-dd',
            'total_comments':1,
            'id': 123456,
            'engagements_by_type': {'url clicks': 111, 'other clicks':222},
            'url': 'https://hi.com/stackoverflow/is/the/best',
            'posts_by_paid_unpaid': {'paid': 1, 'total': 100, 'unpaid': 99},
            'organic_impressions': 1,
            'social_media_impressions': {'facebook': 2, 'twitter': 4, 'instagram': 4, 'twitch': 6}},
            {'date': 'yyyy-mm-dd',
            'total_comments':1,
            'id': 123456,
            'engagements_by_type': {'url clicks': 111, 'other clicks':222},
            'url': 'https://hi.com/stackoverflow/is/the/best',
            'posts_by_paid_unpaid': {'paid': 1, 'total': 100, 'unpaid': 99},
            'organic_impressions': 1,
            'social_media_impressions': {'facebook': 2, 'twitter': 4, 'instagram': 4, 'twitch': 6}},
            {'date': 'yyyy-mm-dd',
            'total_comments':1,
            'id': 123456,
            'engagements_by_type': {'url clicks': 111, 'other clicks':222},
            'url': 'https://hi.com/stackoverflow/is/the/best',
            'posts_by_paid_unpaid': {'paid': 1, 'total': 100, 'unpaid': 99},
            'organic_impressions': 1,
            'social_media_impressions': {'facebook': 2, 'twitter': 4, 'instagram': 4, 'twitch': 6}}
    ] 

def create_plain_dict(ip):
    for i in list(ip):
        if type(ip[i]) == dict: #check whether value associated with that key is dict and if yes then update it with original dict and pop that key
            temp = ip.pop(i) #in this way we are basically converting nested dict into plain dict 
            ip.update(temp)
    return ip

mod_data = list(map(create_plain_dict, data))

df = pd.DataFrame(data)
dataframe看起来像这样


为什么不用示例输出更新您的问题,以帮助人们理解您的问题