要转换为df的列表:
final_list = [{'ID1':{'word':'4', 'talk': '4}}, {'ID2': {'cat':'3', 'dog': '3'}}, {'ID3': {'potatoes':'8', 'height': '6'}}]
预期输出
Word Number Category
0 word 4 ID1
1 talk 4 ID1
2 cat 3 ID2
3 dog 3 ID2
4 potatoes 8 ID3
5 height 6 ID3
我已经创建了一个数据框,可以在其中获取所需的Word和Number列。从这个数据框中,我试图将final_list的“ Category”键添加为第三列。这显然是行不通的,因为循环时我只得到最后一个关键元素。这只是为了显示我的思路。
我的编码尝试
df = pd.DataFrame([(a, b) for item in another_list for a, b in item.items()],
columns=['Word','Number'])
## add the last desired column (failed attempt)
for item in final_list:
for k,v in item.items():
df_events["Category"] = k
答案 0 :(得分:3)
对于元组列表的扁平内部字典,有必要为语句添加下一个:
df = pd.DataFrame([(k,v, a) for item in final_list
for a, b in item.items()
for k, v in b.items()],
columns=['Word','Number','Category'])
print (df)
Word Number Category
0 word 4 ID1
1 talk 4 ID1
2 cat 3 ID2
3 dog 3 ID2
4 potatoes 8 ID3
5 height 6 ID3
答案 1 :(得分:1)
# flatten the dictionary
flat_dict = {key: val for dct in final_list for key, val in dct.items()}
# generate dataframe
df = pd.DataFrame.from_dict(flat_dict).stack().reset_index()
# set column names
df.columns = ['Word', 'Category', 'Number']
print(df)
Word Category Number
0 cat ID2 3
1 dog ID2 3
2 height ID3 6
3 potatoes ID3 8
4 talk ID1 4
5 word ID1 4