我有像下面示例数据一样的数据框。我正在尝试将数据帧中的一行转换为如下所需输出的字典。但是当我使用 to_dict 时,我得到了索引和列值。有谁知道如何将行转换为所需输出的字典?非常感谢任何提示。
Sample data:
print(catStr_df[['Bottle Volume (ml)', 'Pack']][:5])
Bottle Volume (ml) Pack
595 750 12
1889 750 12
3616 1000 12
4422 750 12
5022 750 12
Code:
v = catStr_df[catStr_df['Item Number']==34881][['Bottle Volume (ml)', 'Pack']]\
.drop_duplicates(keep='first').to_dict()
v
Output:
{'Bottle Volume (ml)': {9534: 1000}, 'Pack': {9534: 12}}
Desired output:
{'Bottle Volume (ml)': 1000, 'Pack': 12}
原文由 user3476463 发布,翻译遵循 CC BY-SA 4.0 许可协议
尝试将
.to_dict('records')[0]
添加到您想要的行