Python 读取 Excel 文档某列数据分类统计并绘制盒须图
以下是一个示例代码,可以读取 Excel 文档中的某一列数据,对其进行分类并统计数量,最后绘制盒须图。
import pandas as pd
import matplotlib.pyplot as plt
# 读取 Excel 文件
df = pd.read_excel('data.xlsx')
# 选择需要统计的列
column_to_count = 'Column1'
# 对列进行分类并统计数量
data = df.groupby(column_to_count).size().reset_index(name='count')
# 绘制盒须图
plt.boxplot(data['count'])
plt.title('Boxplot of ' + column_to_count)
plt.show()
在上述代码中,我们使用了 pandas 库来读取 Excel 文件,并使用 groupby() 方法对需要统计的列进行分类并统计数量。最后,我们使用 matplotlib 库来绘制盒须图。
原文地址: https://www.cveoy.top/t/topic/f0ab 著作权归作者所有。请勿转载和采集!