Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count frequency of entries in a pandas column, then plot them in plotly with X axis string lablel

I have the following pandas column:

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

and I want to get this expected output in pandas dataframe:

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

How to do this, it's needed for graph purpose.

edit 1: by referring to the below answers i did some modification of the original code to plot using plotly . Now all the counts are plotted but the X axis label is not coming using this method this is the reason i want the label and the count to be stored in a pd.

reference code

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)
    #)
)
like image 215
Marx Babu Avatar asked Oct 17 '18 10:10

Marx Babu


1 Answers

You might be looking for value counts, which is similar to collections counter.

df['FuncGroup'].value_counts()

For plotting, look at this example:

import pandas as pd

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

s = df['FuncGroup'].value_counts()
s.plot(kind='bar')

dfout = df['FuncGroup'].value_counts().reset_index()
print(dfout)

#  index  FuncGroup
#0   ABC          2
#1   BCD          1

Returns:

enter image description here

like image 122
Anton vBR Avatar answered Sep 30 '22 10:09

Anton vBR