Suppose we have a dictionary
dict = {'list1' = ['a','b','e'],
'list2' = ['a','c'],
'list3' = ['a','b','d']}
How can we create a dataframe whose columns is predefined as below
a b c d e f
list1 1 1 0 0 1 0
list2 1 0 1 0 0 0
list3 1 1 0 1 0 0
Any suggestions would be greatly appreciated. Thanks
This does the trick:
import pandas as pd
def sets_to_dataframe(d, keys):
rows = []
index = []
for label, values in d.items():
index.append(label)
rows.append([int(key in values) for key in keys])
return pd.DataFrame(rows, index=index, columns=keys)
d = {
"list1": ["a", "b", "e"],
"list2": ["a", "c"],
"list3": ["a", "b", "d"],
}
print(
sets_to_dataframe(
d, keys=["a", "b", "c", "d", "e", "f"]
)
)
Output:
a b c d e f
list1 1 1 0 0 1 0
list2 1 0 1 0 0 0
list3 1 1 0 1 0 0
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.