gpt4 book ai didi

python - 将数据帧行转换为字典

转载 作者:太空狗 更新时间:2023-10-29 21:30:23 24 4
gpt4 key购买 nike

我有类似下面示例数据的数据帧。我正在尝试将数据帧中的一行转换为如下所需输出的字典。但是当我使用 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}

最佳答案

尝试将 .to_dict('records')[0] 添加到您想要的行

catStr_df[catStr_df['Item Number']==34881].to_dict('records')[0]

关于python - 将数据帧行转换为字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50575802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com