题目要求
-
利用Pandas建立学生信息二维表,包含姓名、年龄、性别、专业、个人电子设备数量和平均月支出(以一百元为单位)六类信息,至少创建10条具有代表性的记录(10分)。
-
显示最后三行的信息并筛选出个人电子设备超过3的学生(10分)。
-
利用Matplotlab绘制反映月支出和电子设备数量关系的散点图(10分)。
-
绘制反映姓名和年龄的柱状图(10分)。
-
在一个图中绘制每位同学月支出和电子设备的曲线图,并显示图例和必要的提示信息(15分)。
-
按照专业分组并绘制柱状图(15分)。
-
按照专业分组并统计不同性别的人数,绘制合适的图形(15分)。
-
绘制反应年龄的直方图(15分)。
答案
| import pandas as pd |
| import numpy as np |
| import matplotlib.pyplot as plt |
利用Pandas建立学生信息二维表,
- 包含姓名、年龄、性别、专业、个人电子设备数量和平均月支出(以一百元为单位)六类信息,
- 至少创建10条具有代表性的记录(10分)。
| |
| |
| students = pd.DataFrame( columns=['姓名', '年龄', '性别', '专业', '个人电子设备数量', '平均月支出(百元)']) |
| print(students) |
| |
| Empty DataFrame |
| Columns: [姓名, 年龄, 性别, 专业, 个人电子设备数量, 平均月支出(百元)] |
| Index: [] |
| |
| students.drop(students.index,inplace=True) |
| |
| students.loc[len(students.index)] = ['小王', 20, "男","软件工程", 5, 10.20] |
| students.loc[len(students.index)] = ['小屋', 21, "男","计算机科学与技术", 3, 6.24] |
| students.loc[len(students.index)] = ['大黑', 22, "男","软件工程", 3, 7.40] |
| students.loc[len(students.index)] = ['张三', 21, "男","计算机科学与技术", 2, 7.25] |
| students.loc[len(students.index)] = ['李四', 20, "男","大数据", 3, 6.34] |
| students.loc[len(students.index)] = ['小静', 21, "女","大数据", 3, 8.34] |
| students.loc[len(students.index)] = ['小沈', 20, "男","大数据", 3, 6.64] |
| students.loc[len(students.index)] = ['小霍', 20, "男","大数据", 3, 7.08] |
| students.loc[len(students.index)] = ['小苌', 20, "男","计算机科学与技术", 3, 7.20] |
| students.loc[len(students.index)] = ['大费', 19, "女","计算机科学与技术", 4, 8.45] |
| students.loc[len(students.index)] = ['王五', 20, "男","计算机科学与技术", 3, 6.42] |
| students.loc[len(students.index)] = ['老杜', 20, "男","软件工程", 4, 7.33] |
| students.loc[len(students.index)] = ['小怡', 20, "女","软件工程", 4, 9.53] |
| |
| |
| students[['年龄', '个人电子设备数量']] =students[['年龄', '个人电子设备数量']].astype(int) |
| students[['平均月支出(百元)']] =students[['平均月支出(百元)']].astype(float) |
| print(students) |
| 姓名 年龄 性别 专业 个人电子设备数量 平均月支出(百元) |
| 0 小王 20 男 软件工程 5 10.20 |
| 1 小屋 21 男 计算机科学与技术 3 6.24 |
| 2 大黑 22 男 软件工程 3 7.40 |
| 3 张三 21 男 计算机科学与技术 2 7.25 |
| 4 李四 20 男 大数据 3 6.34 |
| 5 小静 21 女 大数据 3 8.34 |
| 6 小沈 20 男 大数据 3 6.64 |
| 7 小霍 20 男 大数据 3 7.08 |
| 8 小苌 20 男 计算机科学与技术 3 7.20 |
| 9 大费 19 女 计算机科学与技术 4 8.45 |
| 10 王五 20 男 计算机科学与技术 3 6.42 |
| 11 老杜 20 男 软件工程 4 7.33 |
| 12 小怡 20 女 软件工程 4 9.53 |
| |
2. 显示最后三行的信息并筛选出个人电子设备超过3的学生(10分)。
| print("最后三行的信息") |
| print(students.tail(3)) |
| print("个人电子设备超过3的学生") |
| print(students[students['个人电子设备数量'] >3]) |
| 最后三行的信息 |
| 姓名 年龄 性别 专业 个人电子设备数量 平均月支出(百元) |
| 10 王五 20 男 计算机科学与技术 3 6.42 |
| 11 老杜 20 男 软件工程 4 7.33 |
| 12 小怡 20 女 软件工程 4 9.53 |
| 个人电子设备超过3的学生 |
| 姓名 年龄 性别 专业 个人电子设备数量 平均月支出(百元) |
| 0 小王 20 男 软件工程 5 10.20 |
| 9 大费 19 女 计算机科学与技术 4 8.45 |
| 11 老杜 20 男 软件工程 4 7.33 |
| 12 小怡 20 女 软件工程 4 9.53 |
3. 利用Matplotlab绘制反映月支出和电子设备数量关系的散点图(10分)。
| |
| x=students["平均月支出(百元)"].values |
| y=students["个人电子设备数量"].values |
| |
| plt.scatter(x,y,alpha=0.5,s=100) |
| |
| plt.xticks(np.arange(int(min(x)-1), int(max(x)+2), 1)) |
| plt.yticks(np.arange(int(min(y)-1), int(max(y)+2), 1)) |
| plt.title("月支出和电子设备数量关系的散点图") |
| plt.xlabel("个人电子设备数量") |
| plt.ylabel("平均月支出(百元)") |

4. 绘制反映姓名和年龄的柱状图(10分)。
| |
| from pylab import * |
| mpl.rcParams['font.sans-serif'] = ['SimHei'] |
| mpl.rcParams['axes.unicode_minus'] = False |
| |
| fig = plt.figure(figsize=(12,4)) |
| |
| |
| plt.subplot(121) |
| |
| x=students["姓名"].values |
| y=students["年龄"].values |
| |
| plt.bar(x,y) |
| |
| plt.title("姓名和年龄的柱状图") |
| plt.yticks(np.arange(15, 26, 1)) |
| plt.xlabel("姓名") |
| plt.ylabel("年龄") |
| |
| plt.ylim((15,25)) |
| |
| |
| plt.subplot(122) |
| |
| xx=students["年龄"].value_counts() |
| labels=students["年龄"].value_counts().index |
| |
| |
| patches,l_text,p_text =plt.pie(xx,labels=labels,autopct='%.1f%%') |
| |
| |
| plt.title("各年龄比例饼图") |
| |
| for t in l_text: |
| t.set_size(20) |
| |
| for t in p_text: |
| t.set_size(15) |
| |
| plt.legend(patches,labels,loc="right",bbox_to_anchor=(1,0,0.3,1)) |
| |
| plt.show() |

5. 在一个图中绘制每位同学月支出和电子设备的曲线图,并显示图例和必要的提示信息(15分)。
| |
| from scipy import interpolate |
| |
| |
| |
| xx=students["姓名"].values |
| x = np.array([num for num in range(len(xx))]) |
| |
| y_money=np.array(students["平均月支出(百元)"].values) |
| y_quantity=np.array(students["个人电子设备数量"].values) |
| |
| |
| |
| xnew = np.arange(0, len(xx)-1, 0.1) |
| |
| func_money = interpolate.interp1d(x, y_money, kind='cubic') |
| func_quantity = interpolate.interp1d(x, y_quantity, kind='cubic') |
| |
| ynew_money = func_money(xnew) |
| func_quantity = func_quantity(xnew) |
| |
| |
| |
| plt.scatter(xx, y_money) |
| plt.scatter(xx, y_quantity) |
| |
| l1=plt.plot(xnew, ynew_money, label='平均月支出(百元)',color="g") |
| l2=plt.plot(xnew, func_quantity,label='个人电子设备数量',color="r") |
| |
| |
| |
| plt.legend(loc='upper center') |
| plt.xlabel("姓名") |
| plt.ylabel("数量") |
| plt.yticks(np.arange(0, max(max(y_money),max(y_quantity))+2, 1)) |
| plt.title("每位同学月支出和电子设备的曲线图") |
| |
| plt.show() |
| |

6. 按照专业分组并绘制柱状图(15分)。
| |
| |
| vals = students.groupby("专业").mean() |
| |
| print(vals) |
| |
| |
| |
| p1=vals.plot.barh(width=0.9) |
| |
| |
| plt.legend(loc='upper right') |
| plt.xticks(np.arange(0, 30, 2)) |
| plt.xlabel("数量") |
| plt.ylabel("专业") |
| plt.title("不同的专业的年龄,设备数量,平均月支出平均值的柱状图") |
| |
| |
| plt.show() |
| 年龄 个人电子设备数量 平均月支出(百元) |
| 专业 |
| 大数据 20.25 3.0 7.100 |
| 计算机科学与技术 20.20 3.0 7.112 |
| 软件工程 20.50 4.0 8.615 |
| |

7. 按照专业分组并统计不同性别的人数,绘制合适的图形(15分)。
| |
| |
| vals = students.groupby(["专业","性别"]).size().unstack() |
| |
| |
| |
| |
| vals.plot.barh() |
| |
| |
| plt.legend(loc='upper right') |
| plt.title("不同专业男女人数统计柱状图") |
| plt.xlabel("数量") |
| plt.ylabel("专业") |
| |
| |
| plt.show() |

8. 绘制反应年龄的直方图(15分)。
| |
| vals = students["年龄"].values |
| |
| |
| |
| plt.hist(vals,color = 'steelblue', edgecolor = 'k') |
| |
| |
| plt.tick_params(top='off', right='off') |
| plt.title("年龄的频数直方图") |
| plt.xlabel("年龄") |
| plt.ylabel("频数") |
| |
| |
| plt.show() |

【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步