简体   繁体   English

在 pandas python 的一个图中绘制多个条形图

[英]Plotting multiple bars in one graph in pandas python

I want to plot bar graph from the dataframe below.我想从下面的 dataframe 中得到 plot 条形图。

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.我想要 plot 条形图,其中我在 y 轴上有URL计数,在 x 轴上有XYZ计数,每个有三个条形。 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.一个栏将显示相应列中所有数字的总和,另一个栏将显示列中非零值的数量,而第三个栏将显示URL列中重复值的计数,例如A出现两次,因此它将计为一个,对于 B 和 C 和 plot 相同,每个条在 x 轴上的结果。我已经设法为前两种情况绘制条形图(下面的代码),但对于第三条我无法绘制。 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")

在此处输入图像描述

You can try with nunique你可以试试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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM