the aim is to construct a dataframe with a dictionary with some modifications:
df = pd.DataFrame({'1-7':[0,0,1],'1-2':[1,0,1],'2-3':[1,0,0],'2-2':[0,1,0],'1-1':[1,0,0],'1-3':[0,1,1],'1-5':[0,1,0]},index=dici.keys())
df
Data:
I got a dictionary like this:
dici={'a':['1-1','1-2','2-3'],'b':['2-2','1-5','1-3'],'c':['1-7','1-2','1-3']}
dici
the union of the list dici.values()
will be used as the column name, and the list dici.keys()
will be used as the index name.
the data explanation in df
:
for example, if the value '1-1'
exits /equals to the name of the column, the value will be 1
, else, the value will be 0
.
CodePudding user response:
You can convert your dict into a Series object, and then join them by any random string and use .str.get_dummies
:
df = pd.Series(dici).str.join('x').str.get_dummies('x')
Output:
>>> df
1-1 1-2 1-3 1-5 1-7 2-2 2-3
a 1 1 0 0 0 0 1
b 0 0 1 1 0 1 0
c 0 1 1 0 1 0 0