绘制数据框架中所有列的直方图

2024-06-10 19:38:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图为我的数据框中的所有列绘制直方图。 我导入了pysparkmatplotlib。 df是我的数据帧变量。 plt是matplotlib.pyplot变量

我能够为单个列绘制直方图,如下所示:

bins, counts = df_maverick.select('ColumnName').rdd.flatMap(lambda x: x).histogram(20)
plt.hist(bins[:-1], bins=bins, weights=counts)

但当我试图为所有变量绘制它时,我遇到了问题。这是到目前为止我的for循环:

for x in range(0, len(df.columns)):
    bins, counts = df.select(x).rdd.flatMap(lambda x: x).histogram(20)
    plt.hist(bins[:-1], bins=bins, weights=counts)

我该怎么做?提前谢谢。


Tags: 数据lambdadfmatplotlib绘制plt直方图select
1条回答
网友
1楼 · 发布于 2024-06-10 19:38:55

问题是您的for循环:

for x in range(0, len(df.columns)):

将在一系列整数上迭代。然后,当您尝试通过以下方式访问列时:

df.select(x)

您将得到一个错误,因为x不是有效的列标识符。

相反,请将循环更改为:

for x in df.columns:

剩下的代码就可以工作了。

相关问题 更多 >