6

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)
    #)
)
3
  • This is simply "Count entries in a column". Then barplot them. I can't see why you'd say "using unique reference of another column". Commented Oct 19, 2018 at 9:46
  • @smci As I updated below in plotly the x-axis prints number 1 2 3 4 5 ...25 which is a count of each unique label. What is want is a print of X-axis label as ABC, BCD, SDS so in this method the count not work. Commented Oct 19, 2018 at 9:50
  • Ok so your new question is how to use the labels as the X-axis. With plotly. That's pretty easy. But it's also a new separate question. In general on SO, the etiquette is not to ask multiple questions in one, especially tacking follow-on questions on. But for this time, please just edit your question title and body. I tried to help you with "Count frequency of entries in a pandas column, then barplot them". By the way the term for ""Count entries in a column using unique reference of another column" is "Count frequency(/ies) in column" Commented Oct 19, 2018 at 23:17

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

8 Comments

i need a one to one mapping so that [ABC, 4] [BCD,5][SDS,3] comes and i can graph it ABC BCD SDS as X axis and value 4 5 3 as Y axis.
@MarxBabu Yes sure. But you don't need that. just use .plot()
@AntonvBR - it is value_counts + ploting, so reopening ;)
@jezrael I dunno man. I think this might be a dupe anyway. Sometimes it is hard to ask the right question right away.
@AntonvBR - Ya, you are right. Btw, I like more s.plot.bar() ;)
|
3

Check if this works for you:

import pandas as pd
import plotly.plotly as py

Sample df:

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

Create new df with counts:

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.

Create plotly bar graph:

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

4 Comments

i have used this and passed this to plotly it prints the value as 1 2 3 5 ...25 it is not useful to print the label in the group.
Your original question didn't ask for ploting. It stopped to get only the count of unique values, which is what the above solution does!!
Later i added the plot details . Plot has no interactive chart so plotly is my need . How this can be solved. it is going crazy still.
This works fine . Thank you for your answer.It is a bit not straightforward still it is the right answer for me . Thank you.If you could just explain new=pd... line of code bit in details it will be helpful learning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.