Python-100-Days/Day76-90/code/5-pandas多层索引计算.ipynb

1001 lines
27 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"import pandas as pd\n",
"\n",
"from pandas import Series,DataFrame"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead tr th {\n",
" text-align: left;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr>\n",
" <th></th>\n",
" <th colspan=\"2\" halign=\"left\">Python</th>\n",
" <th colspan=\"2\" halign=\"left\">En</th>\n",
" <th colspan=\"2\" halign=\"left\">Math</th>\n",
" </tr>\n",
" <tr>\n",
" <th></th>\n",
" <th>期中</th>\n",
" <th>期末</th>\n",
" <th>期中</th>\n",
" <th>期末</th>\n",
" <th>期中</th>\n",
" <th>期末</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>A</th>\n",
" <td>131</td>\n",
" <td>101</td>\n",
" <td>1</td>\n",
" <td>73</td>\n",
" <td>15</td>\n",
" <td>17</td>\n",
" </tr>\n",
" <tr>\n",
" <th>B</th>\n",
" <td>62</td>\n",
" <td>34</td>\n",
" <td>53</td>\n",
" <td>101</td>\n",
" <td>24</td>\n",
" <td>57</td>\n",
" </tr>\n",
" <tr>\n",
" <th>C</th>\n",
" <td>24</td>\n",
" <td>76</td>\n",
" <td>36</td>\n",
" <td>117</td>\n",
" <td>123</td>\n",
" <td>105</td>\n",
" </tr>\n",
" <tr>\n",
" <th>D</th>\n",
" <td>112</td>\n",
" <td>46</td>\n",
" <td>79</td>\n",
" <td>42</td>\n",
" <td>46</td>\n",
" <td>122</td>\n",
" </tr>\n",
" <tr>\n",
" <th>E</th>\n",
" <td>66</td>\n",
" <td>113</td>\n",
" <td>104</td>\n",
" <td>45</td>\n",
" <td>10</td>\n",
" <td>108</td>\n",
" </tr>\n",
" <tr>\n",
" <th>F</th>\n",
" <td>111</td>\n",
" <td>108</td>\n",
" <td>4</td>\n",
" <td>41</td>\n",
" <td>132</td>\n",
" <td>21</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Python En Math \n",
" 期中 期末 期中 期末 期中 期末\n",
"A 131 101 1 73 15 17\n",
"B 62 34 53 101 24 57\n",
"C 24 76 36 117 123 105\n",
"D 112 46 79 42 46 122\n",
"E 66 113 104 45 10 108\n",
"F 111 108 4 41 132 21"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 多层列索引\n",
"df = DataFrame(np.random.randint(0,150,size = (6,6)),index = list('ABCDEF'),\n",
" columns=pd.MultiIndex.from_product([['Python','En','Math'],['期中','期末']]))\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/plain": [
"Python 期中 84.3\n",
" 期末 79.7\n",
"En 期中 46.2\n",
" 期末 69.8\n",
"Math 期中 58.3\n",
" 期末 71.7\n",
"dtype: float64"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# round保留2位小数\n",
"df.mean().round(1)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead tr th {\n",
" text-align: left;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr>\n",
" <th></th>\n",
" <th colspan=\"2\" halign=\"left\">Python</th>\n",
" <th colspan=\"2\" halign=\"left\">En</th>\n",
" <th colspan=\"2\" halign=\"left\">Math</th>\n",
" </tr>\n",
" <tr>\n",
" <th></th>\n",
" <th>期中</th>\n",
" <th>期末</th>\n",
" <th>期中</th>\n",
" <th>期末</th>\n",
" <th>期中</th>\n",
" <th>期末</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>A</th>\n",
" <td>131</td>\n",
" <td>101</td>\n",
" <td>1</td>\n",
" <td>73</td>\n",
" <td>15</td>\n",
" <td>17</td>\n",
" </tr>\n",
" <tr>\n",
" <th>B</th>\n",
" <td>62</td>\n",
" <td>34</td>\n",
" <td>53</td>\n",
" <td>101</td>\n",
" <td>24</td>\n",
" <td>57</td>\n",
" </tr>\n",
" <tr>\n",
" <th>C</th>\n",
" <td>24</td>\n",
" <td>76</td>\n",
" <td>36</td>\n",
" <td>117</td>\n",
" <td>123</td>\n",
" <td>105</td>\n",
" </tr>\n",
" <tr>\n",
" <th>D</th>\n",
" <td>112</td>\n",
" <td>46</td>\n",
" <td>79</td>\n",
" <td>42</td>\n",
" <td>46</td>\n",
" <td>122</td>\n",
" </tr>\n",
" <tr>\n",
" <th>E</th>\n",
" <td>66</td>\n",
" <td>113</td>\n",
" <td>104</td>\n",
" <td>45</td>\n",
" <td>10</td>\n",
" <td>108</td>\n",
" </tr>\n",
" <tr>\n",
" <th>F</th>\n",
" <td>111</td>\n",
" <td>108</td>\n",
" <td>4</td>\n",
" <td>41</td>\n",
" <td>132</td>\n",
" <td>21</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Python En Math \n",
" 期中 期末 期中 期末 期中 期末\n",
"A 131 101 1 73 15 17\n",
"B 62 34 53 101 24 57\n",
"C 24 76 36 117 123 105\n",
"D 112 46 79 42 46 122\n",
"E 66 113 104 45 10 108\n",
"F 111 108 4 41 132 21"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Python</th>\n",
" <th>En</th>\n",
" <th>Math</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>A</th>\n",
" <td>116.0</td>\n",
" <td>37.0</td>\n",
" <td>16.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>B</th>\n",
" <td>48.0</td>\n",
" <td>77.0</td>\n",
" <td>40.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>C</th>\n",
" <td>50.0</td>\n",
" <td>76.5</td>\n",
" <td>114.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>D</th>\n",
" <td>79.0</td>\n",
" <td>60.5</td>\n",
" <td>84.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>E</th>\n",
" <td>89.5</td>\n",
" <td>74.5</td>\n",
" <td>59.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>F</th>\n",
" <td>109.5</td>\n",
" <td>22.5</td>\n",
" <td>76.5</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Python En Math\n",
"A 116.0 37.0 16.0\n",
"B 48.0 77.0 40.5\n",
"C 50.0 76.5 114.0\n",
"D 79.0 60.5 84.0\n",
"E 89.5 74.5 59.0\n",
"F 109.5 22.5 76.5"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# axis = 0代表行\n",
"# axis = 1代表列\n",
"df.mean(axis = 1,level = 0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>期中</th>\n",
" <th>期末</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>A</th>\n",
" <td>49.0</td>\n",
" <td>63.7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>B</th>\n",
" <td>46.3</td>\n",
" <td>64.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>C</th>\n",
" <td>61.0</td>\n",
" <td>99.3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>D</th>\n",
" <td>79.0</td>\n",
" <td>70.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>E</th>\n",
" <td>60.0</td>\n",
" <td>88.7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>F</th>\n",
" <td>82.3</td>\n",
" <td>56.7</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" 期中 期末\n",
"A 49.0 63.7\n",
"B 46.3 64.0\n",
"C 61.0 99.3\n",
"D 79.0 70.0\n",
"E 60.0 88.7\n",
"F 82.3 56.7"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.mean(axis = 1,level = 1).round(1)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead tr th {\n",
" text-align: left;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr>\n",
" <th></th>\n",
" <th colspan=\"2\" halign=\"left\">Python</th>\n",
" <th colspan=\"2\" halign=\"left\">En</th>\n",
" <th colspan=\"2\" halign=\"left\">Math</th>\n",
" </tr>\n",
" <tr>\n",
" <th></th>\n",
" <th>期中</th>\n",
" <th>期末</th>\n",
" <th>期中</th>\n",
" <th>期末</th>\n",
" <th>期中</th>\n",
" <th>期末</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>A</th>\n",
" <td>131</td>\n",
" <td>101</td>\n",
" <td>1</td>\n",
" <td>73</td>\n",
" <td>15</td>\n",
" <td>17</td>\n",
" </tr>\n",
" <tr>\n",
" <th>B</th>\n",
" <td>62</td>\n",
" <td>34</td>\n",
" <td>53</td>\n",
" <td>101</td>\n",
" <td>24</td>\n",
" <td>57</td>\n",
" </tr>\n",
" <tr>\n",
" <th>C</th>\n",
" <td>24</td>\n",
" <td>76</td>\n",
" <td>36</td>\n",
" <td>117</td>\n",
" <td>123</td>\n",
" <td>105</td>\n",
" </tr>\n",
" <tr>\n",
" <th>D</th>\n",
" <td>112</td>\n",
" <td>46</td>\n",
" <td>79</td>\n",
" <td>42</td>\n",
" <td>46</td>\n",
" <td>122</td>\n",
" </tr>\n",
" <tr>\n",
" <th>E</th>\n",
" <td>66</td>\n",
" <td>113</td>\n",
" <td>104</td>\n",
" <td>45</td>\n",
" <td>10</td>\n",
" <td>108</td>\n",
" </tr>\n",
" <tr>\n",
" <th>F</th>\n",
" <td>111</td>\n",
" <td>108</td>\n",
" <td>4</td>\n",
" <td>41</td>\n",
" <td>132</td>\n",
" <td>21</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Python En Math \n",
" 期中 期末 期中 期末 期中 期末\n",
"A 131 101 1 73 15 17\n",
"B 62 34 53 101 24 57\n",
"C 24 76 36 117 123 105\n",
"D 112 46 79 42 46 122\n",
"E 66 113 104 45 10 108\n",
"F 111 108 4 41 132 21"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th></th>\n",
" <th>En</th>\n",
" <th>Math</th>\n",
" <th>Python</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">A</th>\n",
" <th>期中</th>\n",
" <td>1</td>\n",
" <td>15</td>\n",
" <td>131</td>\n",
" </tr>\n",
" <tr>\n",
" <th>期末</th>\n",
" <td>73</td>\n",
" <td>17</td>\n",
" <td>101</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">B</th>\n",
" <th>期中</th>\n",
" <td>53</td>\n",
" <td>24</td>\n",
" <td>62</td>\n",
" </tr>\n",
" <tr>\n",
" <th>期末</th>\n",
" <td>101</td>\n",
" <td>57</td>\n",
" <td>34</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">C</th>\n",
" <th>期中</th>\n",
" <td>36</td>\n",
" <td>123</td>\n",
" <td>24</td>\n",
" </tr>\n",
" <tr>\n",
" <th>期末</th>\n",
" <td>117</td>\n",
" <td>105</td>\n",
" <td>76</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">D</th>\n",
" <th>期中</th>\n",
" <td>79</td>\n",
" <td>46</td>\n",
" <td>112</td>\n",
" </tr>\n",
" <tr>\n",
" <th>期末</th>\n",
" <td>42</td>\n",
" <td>122</td>\n",
" <td>46</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">E</th>\n",
" <th>期中</th>\n",
" <td>104</td>\n",
" <td>10</td>\n",
" <td>66</td>\n",
" </tr>\n",
" <tr>\n",
" <th>期末</th>\n",
" <td>45</td>\n",
" <td>108</td>\n",
" <td>113</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">F</th>\n",
" <th>期中</th>\n",
" <td>4</td>\n",
" <td>132</td>\n",
" <td>111</td>\n",
" </tr>\n",
" <tr>\n",
" <th>期末</th>\n",
" <td>41</td>\n",
" <td>21</td>\n",
" <td>108</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" En Math Python\n",
"A 期中 1 15 131\n",
" 期末 73 17 101\n",
"B 期中 53 24 62\n",
" 期末 101 57 34\n",
"C 期中 36 123 24\n",
" 期末 117 105 76\n",
"D 期中 79 46 112\n",
" 期末 42 122 46\n",
"E 期中 104 10 66\n",
" 期末 45 108 113\n",
"F 期中 4 132 111\n",
" 期末 41 21 108"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 行和列的多层索引,进行转换\n",
"# Stack the prescribed level(s) from columns to index.\n",
"# 从列变成行\n",
"df2 = df.stack(level = 1)\n",
"df2"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead tr th {\n",
" text-align: left;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr>\n",
" <th></th>\n",
" <th colspan=\"6\" halign=\"left\">En</th>\n",
" <th colspan=\"6\" halign=\"left\">Math</th>\n",
" <th colspan=\"6\" halign=\"left\">Python</th>\n",
" </tr>\n",
" <tr>\n",
" <th></th>\n",
" <th>A</th>\n",
" <th>B</th>\n",
" <th>C</th>\n",
" <th>D</th>\n",
" <th>E</th>\n",
" <th>F</th>\n",
" <th>A</th>\n",
" <th>B</th>\n",
" <th>C</th>\n",
" <th>D</th>\n",
" <th>E</th>\n",
" <th>F</th>\n",
" <th>A</th>\n",
" <th>B</th>\n",
" <th>C</th>\n",
" <th>D</th>\n",
" <th>E</th>\n",
" <th>F</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>期中</th>\n",
" <td>1</td>\n",
" <td>53</td>\n",
" <td>36</td>\n",
" <td>79</td>\n",
" <td>104</td>\n",
" <td>4</td>\n",
" <td>15</td>\n",
" <td>24</td>\n",
" <td>123</td>\n",
" <td>46</td>\n",
" <td>10</td>\n",
" <td>132</td>\n",
" <td>131</td>\n",
" <td>62</td>\n",
" <td>24</td>\n",
" <td>112</td>\n",
" <td>66</td>\n",
" <td>111</td>\n",
" </tr>\n",
" <tr>\n",
" <th>期末</th>\n",
" <td>73</td>\n",
" <td>101</td>\n",
" <td>117</td>\n",
" <td>42</td>\n",
" <td>45</td>\n",
" <td>41</td>\n",
" <td>17</td>\n",
" <td>57</td>\n",
" <td>105</td>\n",
" <td>122</td>\n",
" <td>108</td>\n",
" <td>21</td>\n",
" <td>101</td>\n",
" <td>34</td>\n",
" <td>76</td>\n",
" <td>46</td>\n",
" <td>113</td>\n",
" <td>108</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" En Math Python \n",
" A B C D E F A B C D E F A B C D E F\n",
"期中 1 53 36 79 104 4 15 24 123 46 10 132 131 62 24 112 66 111\n",
"期末 73 101 117 42 45 41 17 57 105 122 108 21 101 34 76 46 113 108"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 从行变成列\n",
"df2.unstack(level= 0 )"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead tr th {\n",
" text-align: left;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr>\n",
" <th></th>\n",
" <th colspan=\"2\" halign=\"left\">En</th>\n",
" <th colspan=\"2\" halign=\"left\">Math</th>\n",
" <th colspan=\"2\" halign=\"left\">Python</th>\n",
" </tr>\n",
" <tr>\n",
" <th></th>\n",
" <th>期中</th>\n",
" <th>期末</th>\n",
" <th>期中</th>\n",
" <th>期末</th>\n",
" <th>期中</th>\n",
" <th>期末</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>A</th>\n",
" <td>1</td>\n",
" <td>73</td>\n",
" <td>15</td>\n",
" <td>17</td>\n",
" <td>131</td>\n",
" <td>101</td>\n",
" </tr>\n",
" <tr>\n",
" <th>B</th>\n",
" <td>53</td>\n",
" <td>101</td>\n",
" <td>24</td>\n",
" <td>57</td>\n",
" <td>62</td>\n",
" <td>34</td>\n",
" </tr>\n",
" <tr>\n",
" <th>C</th>\n",
" <td>36</td>\n",
" <td>117</td>\n",
" <td>123</td>\n",
" <td>105</td>\n",
" <td>24</td>\n",
" <td>76</td>\n",
" </tr>\n",
" <tr>\n",
" <th>D</th>\n",
" <td>79</td>\n",
" <td>42</td>\n",
" <td>46</td>\n",
" <td>122</td>\n",
" <td>112</td>\n",
" <td>46</td>\n",
" </tr>\n",
" <tr>\n",
" <th>E</th>\n",
" <td>104</td>\n",
" <td>45</td>\n",
" <td>10</td>\n",
" <td>108</td>\n",
" <td>66</td>\n",
" <td>113</td>\n",
" </tr>\n",
" <tr>\n",
" <th>F</th>\n",
" <td>4</td>\n",
" <td>41</td>\n",
" <td>132</td>\n",
" <td>21</td>\n",
" <td>111</td>\n",
" <td>108</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" En Math Python \n",
" 期中 期末 期中 期末 期中 期末\n",
"A 1 73 15 17 131 101\n",
"B 53 101 24 57 62 34\n",
"C 36 117 123 105 24 76\n",
"D 79 42 46 122 112 46\n",
"E 104 45 10 108 66 113\n",
"F 4 41 132 21 111 108"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2.unstack(level = 1)"
]
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}