我有一个字典,我想创建一个数据框,其中的列都是每个键的各个值。例如,如果字典如下所示:
d = {'gender': 'female',
'company': ['nike', 'adidas'],
'location': ['chicago', 'miami'],
'plan': 'high'}
我希望数据框看起来像这样:
female nike adidas chicago miami high
1 1 1 1 1 1
答案 0 :(得分:1)
您可以执行explode
+ value_counts
df=pd.Series(d).explode().value_counts().to_frame(0).T
chicago female nike miami high adidas
0 1 1 1 1 1 1
答案 1 :(得分:0)
这是一个幼稚的解决方案,但它可以工作。这个想法是:
d
字典组织为反字典,例如{'female': 1,
'nike': 1,
'adidas': 1,
'chicago': 1,
'miami': 1,
'high': 1}
这是代码:
# 1. create list to count
out = []
for value in d.values():
if isinstance(value, list):
out.extend(value)
else:
out.append(value)
# out = ['female', 'nike', 'adidas', 'chicago', 'miami', 'high']
# 2. count occurrence of each unique item in this out list
from collections import Counter
count = Counter(out)
# 3. pandas df from dictionary
import pandas as pd
pd.DataFrame([Counter(out)])
# output:
# female nike adidas chicago miami high
# 1 1 1 1 1 1