gpt4 book ai didi

python - 从字典创建数据帧,键不作为索引

转载 作者:太空宇宙 更新时间:2023-11-03 15:52:28 27 4
gpt4 key购买 nike

我有一个基本字典,可以统计数据出现的次数。例如亚当:10,贝丝:3,...,扎克:1

如果我执行df = pd.DataFrame([dataDict]).T,那么字典中的键将成为数据帧的索引,并且我只有 1 个真实的数据列。我已经查过了,没有找到解决此问题的方法,因此我们将不胜感激。

编辑:更多详细信息

字典是由另一个数据帧的计数函数形成的,例如dataDict = df1.Name.value_counts().to_dict()

这是我的预期输出。

       | Name | Count
------ | -----|------
0 | Adam | 10
------ | -----|------
1 | Beth | 3

我现在得到的是:

       | Count
-----|------
Adam | 10
-----|------
Beth | 3

最佳答案

尝试reset_index

dataDict = dict(Adam=10, Beth=3, Zack=1)
df = pd.Series(dataDict).rename_axis('Name').reset_index(name='Count')
df

enter image description here

关于python - 从字典创建数据帧,键不作为索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41151595/

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