简体   繁体   中英

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:

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:

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):

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.

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. This gives a transposed version of your desired output. Transpose the result and fill NaN with False gives what you need:

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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