怎么统计出数据集ObesityDataSet_raw_and_data中family_history_with_overweight、FAVC、SMOKE和SCC中yes和no的个数并画出条形图
时间: 2024-02-25 20:57:39 浏览: 71
php画数据统计图折线图饼形图条形图
3星 · 编辑精心推荐
可以使用Python的pandas库来读取数据集,并且使用value_counts()方法来统计family_history_with_overweight、FAVC、SMOKE和SCC中yes和no出现的次数。然后,可以使用Python的matplotlib库来画出条形图。以下是示例代码:
```python
import pandas as pd
import matplotlib.pyplot as plt
# 读取数据集
data = pd.read_csv('ObesityDataSet_raw_and_data.csv')
# 统计family_history_with_overweight、FAVC、SMOKE和SCC中yes和no出现的次数
counts1 = data['family_history_with_overweight'].value_counts()
counts2 = data['FAVC'].value_counts()
counts3 = data['SMOKE'].value_counts()
counts4 = data['SCC'].value_counts()
# 绘制条形图
fig, axs = plt.subplots(2, 2, figsize=(10, 8))
axs[0, 0].bar(counts1.index, counts1.values)
axs[0, 0].set_title('Family History Counts')
axs[0, 0].set_xlabel('Family History')
axs[0, 0].set_ylabel('Count')
axs[0, 1].bar(counts2.index, counts2.values)
axs[0, 1].set_title('FAVC Counts')
axs[0, 1].set_xlabel('FAVC')
axs[0, 1].set_ylabel('Count')
axs[1, 0].bar(counts3.index, counts3.values)
axs[1, 0].set_title('SMOKE Counts')
axs[1, 0].set_xlabel('SMOKE')
axs[1, 0].set_ylabel('Count')
axs[1, 1].bar(counts4.index, counts4.values)
axs[1, 1].set_title('SCC Counts')
axs[1, 1].set_xlabel('SCC')
axs[1, 1].set_ylabel('Count')
plt.tight_layout()
# 显示图形
plt.show()
```
运行代码后,会弹出一个窗口显示四个条形图,分别对应family_history_with_overweight、FAVC、SMOKE和SCC。其中,x轴表示yes和no,y轴表示出现的次数。
阅读全文