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)
#)
)
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With