简体   繁体   English

从字典列表中提取信息

[英]Extract information from a list of dictionaries

I have a list of dictionaries:我有一个字典列表:

movies['genres'].head()

where each line looks like:每行看起来像:

   0     [{'id': 16, 'name': 'Animation'}, {'id': 35, 'name': 'Comedy'}, {'id': 10751, 'name': 'Family'}]
1    [{'id': 12, 'name': 'Adventure'}, {'id': 14, 'name': 'Fantasy'}, {'id': 10751, 'name': 'Family'}]
2                                     [{'id': 10749, 'name': 'Romance'}, {'id': 35, 'name': 'Comedy'}]
3        [{'id': 35, 'name': 'Comedy'}, {'id': 18, 'name': 'Drama'}, {'id': 10749, 'name': 'Romance'}]
4                                                                       [{'id': 35, 'name': 'Comedy'}]
Name: genres, dtype: object  

     

I would like to save it in a data frame where one column is 'id' and the rows are the id values and another column 'name' where the rows are the name values.我想将它保存在一个数据框中,其中一列是“id”,行是 id 值,另一列是“名称”,其中行是名称值。 I tried with:我试过:

pd.DataFrame(movies['genres'])

However when I ran it I obtained:但是,当我运行它时,我得到了:

        genres
0   [{'id': 16, 'name': 'Animation'}, {'id': 35, 'name': 'Comedy'}, {'id': 10751, 'name': 'Family'}]
1   [{'id': 12, 'name': 'Adventure'}, {'id': 14, 'name': 'Fantasy'}, {'id': 10751, 'name': 'Family'}]
2   [{'id': 10749, 'name': 'Romance'}, {'id': 35, 'name': 'Comedy'}]

Could you help me?你可以帮帮我吗? Regards问候

You should use the command .from_dict() as described here您应该使用此处所述的命令.from_dict()

df = pd.DataFrame.from_dict(movies["genres"])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM