0

I want to plot bar graph from the dataframe below.

df2 = pd.DataFrame({'URL': ['A','A','B','B','C','C'],
                    'X': [5,0,7,1,0,6],
                    'Y': [21,0,4,7,9,0],
                    'Z':[11,0,8,4,0,0]})

I want to plot bar graph in which I have URL counts on y-axis and X , Y, Z on x-axis with three bars for each. One bar will show the total sum of all the numbers in the respective column and another bar will show number of non zero values in column while the third bar will show the count of duplicate values in the URL column like A comes two times so it will count as one, same for B and C and plot the result on x-axis with each bar.I have managed to draw bar graph for the first two cases (code below) but for third bar I'm unable to draw. If anyone can help me in this case. Thank you

df2.melt("URL").\
groupby("variable").\
agg(sums=("value", "sum"),
    nz=("value", lambda x: sum(x != 0))).\
plot(kind="bar")

enter image description here

1 Answer 1

1

You can try with nunique

df2.melt("URL").\
    groupby("variable").\
    agg(sums=("value", "sum"),
        nz=("value", lambda x: sum(x != 0)),
        dup = ("URL", "nunique"))
Out[874]: 
          sums  nz  dup
variable               
X           19   4    3
Y           41   4    3
Z           23   3    3
Sign up to request clarification or add additional context in comments.

Comments

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.