4

I have a df column where its data are only categorical (e.g., [a, b, c, a, a, d, c, b, etc]). I want to plot the count of these data using plotly count bar (bar chart).

I have calculated the count of the data using df.groupby('<col_name>')['<col_name>'].count(), but this returns a series data structure so I will only have the count data (1-D).

How can I get the count result and the corresponding data item in the resulting output efficiently?

I want to get this output and plot the bar chart using Plotly:

import plotly.express as px

fig = px.bar(count_df, x="<col_name>", y="count", color="count", title="----------")
fig.show()
1
  • I think you need this. Commented Mar 16, 2021 at 10:22

4 Answers 4

4

It doesn't get more efficient than this:

df['x'].plot(kind = 'hist')

enter image description here

And NO, this isn't matplotlib, but rather a figure constructed directly from a pandas dataframe using plotly as a backend. And yes, it's awesome!

Complete code:

import random
import pandas as pd
pd.options.plotting.backend = "plotly"
random.seed(7)

df = pd.DataFrame({'x':[random.choice(list('abcde')) for i in range(25)]})
fig = df['x'].plot(kind = 'hist')
fig.layout.bargap = 0
fig.show()
Sign up to request clarification or add additional context in comments.

1 Comment

While this is efficient, the x-axis values for a histogram need to be at least ordered, if not numeric. Also, the y-axis values need to be continuous. Here, the x-axis is categorical and the y-axis is discrete. So we need a bar or column chart, not a histogram.
3

Answering my own question.

I found a solution by converting the result of value_counts(returns a Series) to a pd DataFrame. Ref : SO question and answers

import plotly.express as px

new_df = df['<col_name>'].value_counts().rename_axis('<col_name>').reset_index(name='counts')

fig = px.bar(new_df, x="<col_name>", y="counts", color="counts", title="----------")
fig.show()

Comments

1

Should be able to use .index of the result to give you the values for x-axis and the series itself for the y-axis.

Also, I think using df[‘col_name’].value_counts() is probably what you want to use here.

Comments

0

You can get it easily by using hist(), for example:

df['<col_name>'].hist()

and also you can see the abs frequency.

Another way to do the same is:

df['<col_name>'].value_counts().plot(kind='bar')

1 Comment

Thank you but I want to use Plotly and not the Pandas builtin stuff

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.