如何为列(A组)的某一行绘制具有特定列(english_score)的直方图,并将条柱数设置为20

how to plot a histogram with a certain column(english_score) for a certain row of a column(group a) and set the number of bins to 20

提问人:esme 提问时间:11/14/2023 更新时间:11/15/2023 访问量:40

问:

我有一个数据集,我必须将其转换为数据框。这些数据框有 5 列(性别、组、课程、英语分数final_score)列组有 5 个唯一组(A 组、B 组......E组)。我需要的是将 a 组的直方图绘制在 english_score 上,并将条柱数设置为 20。

Python Pandas DataFrame 直方

评论

0赞 D.L 11/14/2023
到目前为止你尝试过什么?该问题需要足够的代码才能获得最小的可重现示例
0赞 esme 11/15/2023
df.hist(column=[[“english_score”, “group”== “A 组”]])

答:

1赞 ayekaunic 11/15/2023 #1

下面是一个示例代码:

import pandas as pd
import matplotlib.pyplot as plt

# assuming you have already converted your data to a data frame named df
df = df.reset_index()

# filter the rows for group a
group_a = df[df['group'] == 'group a']

# plot the histogram for english_score with 20 bins
plt.hist(group_a['english_score'], bins=20)
plt.title('English Score Histogram for Group A')
plt.xlabel('English Score')
plt.ylabel('Frequency')
plt.show()

此代码首先筛选数据框中与组 a 对应的行。然后,它使用 pandas 库绘制具有 20 个条柱的组 a 的“english_score”列的直方图。最后,它使用 matplotlib 库显示直方图。

您需要编写的实际代码可能会有所不同,具体取决于数据的结构及其在数据框中的存储方式。话虽如此,这个例子应该让你对如何处理这个问题有一个大致的了解。