简体   繁体   English

列表中的几个字典到数据框的列

[英]Several dictionaries in a list to columns of a dataframe

I have a sample of list of dictionaries as given below我有一个字典列表示例,如下所示

NaN = np.nan
cols = [ {'A':['Orange', 'Lemon', NaN, 'Peach']},
         {'B':['Tiger', NaN, NaN, 'Lion']},
         {'C':['London', 'New York', 'Bangalore', NaN]},
         {'D':['Cricket', 'BaseBall', 'Football', 'Tennis']}
       ]

I want to convert this to columns of dataframe.我想将其转换为数据框列。 The keys of the dictionary should be the column name.字典的键应该是列名。 The values should be the corresponding rows of the dataframe.这些值应该是数据帧的相应行。

The final output should look like the image shown below.最终输出应如下图所示。

在此处输入图片说明

Any help is greatly appreciated.任何帮助是极大的赞赏。 Thank you!谢谢!

import pandas as pd

cols = [ {'A':['Orange', 'Lemon', 'NaN', 'Peach']},
         {'B':['Tiger', 'NaN', 'NaN', 'Lion']},
         {'C':['London', 'New York', 'Bangalore', 'NaN']},
         {'D':['Cricket', 'BaseBall', 'Football', 'Tennis']}
       ]
       
df = pd.DataFrame({k: v for d in cols for k, v in d.items()})

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

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