简体   繁体   English

从具有 True 的列列表的字典创建熊猫数据框

[英]Creating a pandas dataframe from dict of lists of columns which have True

I would like to take a dictionary of 'item': [list_of_True_column_labels] that looks like this:我想拿一本'item': [list_of_True_column_labels]字典,看起来像这样:

pre_df  = {'item1':['a','b','c'], 'item2':['c','d'], 'item3':['a', 'c', 'd', 'e'], 'item4':['e']}

and turn it into a pandas dataframe of bool like this:并将其转换为bool的 pandas 数据框,如下所示:

index   A      B      C      D      E
item1  True   True   True   False  False
item2  False  False  True   True   False
item3  True   False  True   True   True
item4  False  False  False  False  True

1) I've tried this (from StackOverflow): 1)我试过这个(来自StackOverflow):

pd.DataFrame(dict([(k,pd.Series(v)) for k,v in pre_df.items()]))

but that gives me an incorrect dataframe:但这给了我一个不正确的数据框:

    item1 item2 item3 item4
0     a     c     a     e
1     b     d     c   NaN
2     c   NaN     d   NaN
3   NaN   NaN     e   NaN

2) using pd.melt() doesn't seem to be the correct approach. 2) 使用pd.melt()似乎不是正确的方法。

You can loop through the dictionary and convert each value to a Series object with the original list as the index and value to be True , and then call the DataFrame.from_dict() method.您可以遍历字典并将每个值转换为以原始列表作为索引和值为True的 Series 对象,然后调用DataFrame.from_dict()方法。 This gives a transposed version of your desired output.这提供了所需输出的转置版本。 Transpose the result and fill NaN with False gives what you need:转置结果并用False填充 NaN 给出您需要的内容:

pd.DataFrame.from_dict({k: pd.Series(True, v) for k, v in pre_df.items()}).T.fillna(False)

在此处输入图片说明

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

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