gpt4 book ai didi

python - 将字典放入数据框 - python

转载 作者:太空宇宙 更新时间:2023-11-03 14:06:33 25 4
gpt4 key购买 nike

我有一个名为“性别”的列表,其中我使用 Counter 计算了值的所有出现次数:

gender = ['2',
'Female,',
'All Female Group,',
'All Male Group,',
'Female,',
'Couple,',
'Mixed Group,'....]

gender_count = Counter(gender)
gender_count
Counter({'2': 1,
'All Female Group,': 222,
'All Male Group,': 119,
'Couple,': 256,
'Female,': 1738,
'Male,': 2077,
'Mixed Group,': 212,
'NA': 16})

我想将这个字典放入 pandas Dataframe 中。我用过 pd.series( Convert Python dict into a dataframe ):

s = pd.Series(gender_count, name='gender count')
s.index.name = 'gender'
s.reset_index()

这给了我想要的数据框,但我不知道如何将这些步骤保存到 pandas DataFrame 中。我也尝试使用 DataFrame.from_dict()

s2 = pd.DataFrame.from_dict(gender_count, orient='index')

但这会创建一个以性别类别为索引的数据框。

我最终想使用性别类别和饼图的计数。

最佳答案

跳过中间步骤

gender = ['2',
'Female',
'All Female Group',
'All Male Group',
'Female',
'Couple',
'Mixed Group']

pd.value_counts(gender)

Female 2
2 1
Couple 1
Mixed Group 1
All Female Group 1
All Male Group 1
dtype: int64

关于python - 将字典放入数据框 - python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43099154/

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