作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
def draw_piecharts(df, variables, n_rows, n_cols):
df[variables].value_counts.plot(kind='pie', layout=(n_rows,n_cols), subplots=True)
plt.show()
def main():
util.draw_piecharts(df, [ 'TARGET', 'BanruptcyInd'], 1,2)
if __name__ == "__main__":
main()
不幸的是,我的函数不计算,因为数据帧没有属性 value_counts()
,而 value_counts 是我知道如何在饼图中绘制分布的唯一方法。这是正在绘制的变量示例:
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
11 0
12 1
13 0
14 0
15 0
16 0
17 1
18 0
19 0
20 0
21 1
22 0
23 0
24 1
25 0
26 1
27 0
28 0
29 0
Name: TARGET, dtype: int64
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
11 0
12 0
13 0
14 0
15 0
16 0
17 0
18 0
19 0
20 0
21 0
22 0
23 0
24 0
25 0
26 0
27 0
28 0
29 0
最佳答案
虽然 value_counts
是一个 Series 方法,但可以使用 DataFrame.apply
轻松将其应用于 DataFrames 内的 Series。在你的情况下。例如,
df[variables].apply(pd.value_counts).plot(kind='pie', layout=(n_rows,n_cols), subplots=True)
(假设 pandas
已导入为 pd
)。
一个完整的例子:
import pandas as pd
a = pd.DataFrame({'a': [1,0,0,0,1,1,0,0,1,0,1,1,1],'b': [1,0,0,0,1,1,0,0,1,0,0,0,0]})
a.apply(pd.value_counts).plot.pie(subplots=True)
关于python - 无法绘制饼图的值计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36963502/
有人可以帮我理解为什么我的饼图百分比计算不正确吗?看截图: 根据我的计算,如 RHS 上所示,支出百分比应为 24.73%。传递给 Highcharts 的值如下:- 花费:204827099.36-
我正在制作圆环饼图。 我设置数据的颜色并获取钻取(它是保存外部切片的数据和配置的对象)切片的颜色为同一组颜色。我想设置要在向下钻取对象中使用的不同颜色数组。请参阅附图(外层切片有两种颜色:橙色和透明)
我是一名优秀的程序员,十分优秀!