假设我有一个目录列表:
my lists = [
{'rank': 2, 'keyword_name': 'mens wallet', 'volume': 456677, 'asin': 'B01MG0ORBL'
},
{'rank': 18, 'keyword_name': 'mens wallet', 'volume': 456677, 'asin': 'B0735C9RDZ'
},
{'rank': 21, 'keyword_name': 'mens wallet', 'volume': 456677, 'asin': 'B07FPVR858'
},
{'rank': 126, 'keyword_name': 'mens wallet', 'volume': , 'asin': 'B01MG0ORBL'
},
{'rank': 128, 'keyword_name': 'mens wallet', 'volume': 456677, 'asin': 'B0735C9RDZ'
},
{'rank': 136, 'keyword_name': 'mens wallet', 'volume': 456677, 'asin': 'B07FPVR858'
},
{'rank': 19, 'keyword_name': 'leather wallets', 'volume': , 'asin': 'B0735C9RDZ'
},
{'rank': 10, 'keyword_name': 'wallets for men', 'volume': 566, 'asin': 'B07FPVR858'
},
{'rank': 16, 'keyword_name': 'wallets for men', 'volume': 566, 'asin': 'B0735C9RDZ'
},
]
我想按asin和keyword\u name进行分组,因为它们在dict列表中出现了多次,所以我的目标是创建一个如下所示的数据帧:
**keyword_name volume B01MG0ORBL B0735C9RDZ B07FPVR858** // column headers
mens wallet 456677 2 126 18 128 19 16 21 10
leather wallets 23
wallets for men 566 16 10
所以一开始我想
d = [{d['asin']:d['rank'] for d in l} for l in my_lists]
pd.dataframe(d)
// save as xlsx file
writer = pd.ExcelWriter(f"{path}/sheet.xlsx", engine="xlsxwriter")
d.to_excel(
writer, sheet_name="Organic", startrow=0, header=True, index=False
)
但不可能,因为它将遇到错误TypeError: string indices must be integers
.