{ "cells": [ { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Exam 1Exam 2Exam 3
Student
Thorny1009080
Mac8899111
Farva455667
Rabbit596167
Ursula737983
Foster8997101
\n", "
" ], "text/plain": [ " Exam 1 Exam 2 Exam 3\n", "Student \n", "Thorny 100 90 80\n", "Mac 88 99 111\n", "Farva 45 56 67\n", "Rabbit 59 61 67\n", "Ursula 73 79 83\n", "Foster 89 97 101" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "sgs = pd.read_csv('super-grades.csv', index_col=0)\n", "sgs" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Farva': 56.0,\n", " 'Foster': 95.666666666666671,\n", " 'Mac': 99.333333333333329,\n", " 'Rabbit': 62.333333333333336,\n", " 'Thorny': 90.0,\n", " 'Ursula': 78.333333333333329}" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{stud: np.mean(sgs.loc[stud]) for stud in sgs.index}" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Farva 56.000000\n", "Foster 95.666667\n", "Mac 99.333333\n", "Rabbit 62.333333\n", "Thorny 90.000000\n", "Ursula 78.333333\n", "dtype: float64" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "avgs = pd.Series({stud: np.mean(sgs.loc[stud]) for stud in sgs.index})\n", "avgs" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Exam 1Exam 2Exam 3avg
Student
Thorny100908090.000000
Mac889911199.333333
Farva45566756.000000
Rabbit59616762.333333
Ursula73798378.333333
Foster899710195.666667
\n", "
" ], "text/plain": [ " Exam 1 Exam 2 Exam 3 avg\n", "Student \n", "Thorny 100 90 80 90.000000\n", "Mac 88 99 111 99.333333\n", "Farva 45 56 67 56.000000\n", "Rabbit 59 61 67 62.333333\n", "Ursula 73 79 83 78.333333\n", "Foster 89 97 101 95.666667" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sgs['avg'] = avgs\n", "sgs" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "75.666666666666671" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ " np.mean(sgs['Exam 1'])" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Exam 1': {'Item Avg': 75.666666666666671},\n", " 'Exam 2': {'Item Avg': 80.333333333333329},\n", " 'Exam 3': {'Item Avg': 84.833333333333329},\n", " 'avg': {'Item Avg': 80.277777777777771}}" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "item_avgs = {col: {'Item Avg': np.mean(sgs[col])} for col in sgs.columns}\n", "item_avgs" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Exam 1Exam 2Exam 3avg
Item Avg75.66666780.33333384.83333380.277778
\n", "
" ], "text/plain": [ " Exam 1 Exam 2 Exam 3 avg\n", "Item Avg 75.666667 80.333333 84.833333 80.277778" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "item_avgs_df = pd.DataFrame(item_avgs)\n", "item_avgs_df" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Exam 1Exam 2Exam 3avg
Thorny100.00000090.00000080.00000090.000000
Mac88.00000099.000000111.00000099.333333
Farva45.00000056.00000067.00000056.000000
Rabbit59.00000061.00000067.00000062.333333
Ursula73.00000079.00000083.00000078.333333
Foster89.00000097.000000101.00000095.666667
Item Avg75.66666780.33333384.83333380.277778
\n", "
" ], "text/plain": [ " Exam 1 Exam 2 Exam 3 avg\n", "Thorny 100.000000 90.000000 80.000000 90.000000\n", "Mac 88.000000 99.000000 111.000000 99.333333\n", "Farva 45.000000 56.000000 67.000000 56.000000\n", "Rabbit 59.000000 61.000000 67.000000 62.333333\n", "Ursula 73.000000 79.000000 83.000000 78.333333\n", "Foster 89.000000 97.000000 101.000000 95.666667\n", "Item Avg 75.666667 80.333333 84.833333 80.277778" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sgs = sgs.append(item_avgs_df)\n", "sgs" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Exam 1Exam 2Exam 3avgGrade
Thorny100.00000090.00000080.00000090.000000A
Mac88.00000099.000000111.00000099.333333A
Farva45.00000056.00000067.00000056.000000D
Rabbit59.00000061.00000067.00000062.333333D
Ursula73.00000079.00000083.00000078.333333C
Foster89.00000097.000000101.00000095.666667A
Item Avg75.66666780.33333384.83333380.277778B
\n", "
" ], "text/plain": [ " Exam 1 Exam 2 Exam 3 avg Grade\n", "Thorny 100.000000 90.000000 80.000000 90.000000 A\n", "Mac 88.000000 99.000000 111.000000 99.333333 A\n", "Farva 45.000000 56.000000 67.000000 56.000000 D\n", "Rabbit 59.000000 61.000000 67.000000 62.333333 D\n", "Ursula 73.000000 79.000000 83.000000 78.333333 C\n", "Foster 89.000000 97.000000 101.000000 95.666667 A\n", "Item Avg 75.666667 80.333333 84.833333 80.277778 B" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sgs['Grade'] = np.where(sgs['avg'] >= 90, 'A',\n", " np.where(sgs['avg'] >= 80, 'B',\n", " np.where(sgs['avg'] >= 70, 'C',\n", " np.where(sgs['avg'] >= 60, 'D',\n", " 'D'))))\n", "sgs" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Grade\n", "A 3\n", "B 1\n", "C 1\n", "D 2\n", "Name: Grade, dtype: int64" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sgs['Grade'].groupby(sgs['Grade']).count()\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }