gpt4 book ai didi

python - 计算 pandas 列中条目的频率,然后使用 X 轴字符串标签将它们绘制成图

转载 作者:太空狗 更新时间:2023-10-30 02:52:34 25 4
gpt4 key购买 nike

我有以下 Pandas 专栏:

FuncGroup
ABC
ABC
ABC
ABC
BCD
BCD
BCD
SDS
SDS
ABC
BCD
SDS
BCD

我想在 pandas dataframe 中得到这个预期的输出:

pd['FunctionGroup','FunctionCount']
ABC 4
BCD 5
SDS 3

如何做到这一点,这是图表目的所必需的。

编辑 1:引用下面的答案我对原代码做了一些修改使用 plotly 绘制。现在所有计数都已绘制,但 X 轴标签不会使用此方法,这就是我希望将标签和计数存储在 pd 中的原因。

引用代码

otrace1 =go.Bar(
#x=stock_opt_pe.index
x=datalist['Function group'].nunique(),
y=datalist['Function group'].value_counts(),
text=datalistFg, # dont know what to give here to get a X axis label
textposition = 'auto',
#xaxis-type (enumerated: “-” | “linear” | “log” | “date” | “category” )
#xaxis-type (enumerated: “-” | “linear” | “log” | “date” | “category” )
#name='Function Group Vx RespPerson',
#orientation = 'v',
#marker = dict(
#color = 'rgba(224, 224, 224, 0.6)',
#line = dict(
#color = 'rgba(246, 250, 206, 1.0)',
#color = 'rgb(60, 60, 60)',
#width = 0)
#)
)

最佳答案

检查这是否适合您:

import pandas as pd
import plotly.plotly as py

样本 df:

raw =pd.DataFrame({'FuncGroup':[
'ABC',
'ABC',
'ABC',
'ABC',
'BCD',
'BCD',
'BCD',
'SDS',
'SDS',
'ABC',
'BCD',
'SDS',
'BCD']})

用计数创建新的 df:

s = raw['FuncGroup'].value_counts() ## Counts the occurrence of unqiue elements and stores in a variable called "s" which is series type
new = pd.DataFrame({'FuncGroup':s.index, 'Count':s.values}) ## Converting series type to pandas df as plotly accepts dataframe as input. The two columns of df is FuncGroup which is being made by index of series and new variable called count which is made by values of series s.

创建条形图:

py.iplot(new, filename='basic-bar')

关于python - 计算 pandas 列中条目的频率,然后使用 X 轴字符串标签将它们绘制成图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52852170/

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