|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "b1b8d3a0-09e3-453d-8c19-4e8c60be4910", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "Chapter 21\n", |
| 9 | + "# 用列表构造多层行标签\n", |
| 10 | + "Book_1《编程不难》 | 鸢尾花书:从加减乘除到机器学习 " |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": 1, |
| 16 | + "id": "2691e1e9-8ffd-4689-ad39-95a3a620125f", |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [], |
| 19 | + "source": [ |
| 20 | + "import pandas as pd\n", |
| 21 | + "import numpy as np" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "code", |
| 26 | + "execution_count": 2, |
| 27 | + "id": "4b43d5ef-a409-40c6-916e-747aa4e12c72", |
| 28 | + "metadata": {}, |
| 29 | + "outputs": [], |
| 30 | + "source": [ |
| 31 | + "# 创建列表、数据\n", |
| 32 | + "index_arrays = [['A','A','B','B','C','C','D','D'], \n", |
| 33 | + " range(1,9)]\n", |
| 34 | + "data = np.random.randint(0,9,size=(8,4))" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "code", |
| 39 | + "execution_count": 3, |
| 40 | + "id": "0416f4b6-08ac-47d1-93bc-877ac55e3a46", |
| 41 | + "metadata": {}, |
| 42 | + "outputs": [], |
| 43 | + "source": [ |
| 44 | + "# 创建多层行索引\n", |
| 45 | + "row_idx = pd.MultiIndex.from_arrays(index_arrays, \n", |
| 46 | + " names=['I','II'])" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "code", |
| 51 | + "execution_count": 4, |
| 52 | + "id": "77fa954e-adac-4675-9549-c41bd5394d23", |
| 53 | + "metadata": {}, |
| 54 | + "outputs": [ |
| 55 | + { |
| 56 | + "data": { |
| 57 | + "text/html": [ |
| 58 | + "<div>\n", |
| 59 | + "<style scoped>\n", |
| 60 | + " .dataframe tbody tr th:only-of-type {\n", |
| 61 | + " vertical-align: middle;\n", |
| 62 | + " }\n", |
| 63 | + "\n", |
| 64 | + " .dataframe tbody tr th {\n", |
| 65 | + " vertical-align: top;\n", |
| 66 | + " }\n", |
| 67 | + "\n", |
| 68 | + " .dataframe thead th {\n", |
| 69 | + " text-align: right;\n", |
| 70 | + " }\n", |
| 71 | + "</style>\n", |
| 72 | + "<table border=\"1\" class=\"dataframe\">\n", |
| 73 | + " <thead>\n", |
| 74 | + " <tr style=\"text-align: right;\">\n", |
| 75 | + " <th></th>\n", |
| 76 | + " <th></th>\n", |
| 77 | + " <th>X1</th>\n", |
| 78 | + " <th>X2</th>\n", |
| 79 | + " <th>X3</th>\n", |
| 80 | + " <th>X4</th>\n", |
| 81 | + " </tr>\n", |
| 82 | + " <tr>\n", |
| 83 | + " <th>I</th>\n", |
| 84 | + " <th>II</th>\n", |
| 85 | + " <th></th>\n", |
| 86 | + " <th></th>\n", |
| 87 | + " <th></th>\n", |
| 88 | + " <th></th>\n", |
| 89 | + " </tr>\n", |
| 90 | + " </thead>\n", |
| 91 | + " <tbody>\n", |
| 92 | + " <tr>\n", |
| 93 | + " <th rowspan=\"2\" valign=\"top\">A</th>\n", |
| 94 | + " <th>1</th>\n", |
| 95 | + " <td>1</td>\n", |
| 96 | + " <td>8</td>\n", |
| 97 | + " <td>2</td>\n", |
| 98 | + " <td>2</td>\n", |
| 99 | + " </tr>\n", |
| 100 | + " <tr>\n", |
| 101 | + " <th>2</th>\n", |
| 102 | + " <td>6</td>\n", |
| 103 | + " <td>5</td>\n", |
| 104 | + " <td>2</td>\n", |
| 105 | + " <td>3</td>\n", |
| 106 | + " </tr>\n", |
| 107 | + " <tr>\n", |
| 108 | + " <th rowspan=\"2\" valign=\"top\">B</th>\n", |
| 109 | + " <th>3</th>\n", |
| 110 | + " <td>8</td>\n", |
| 111 | + " <td>4</td>\n", |
| 112 | + " <td>7</td>\n", |
| 113 | + " <td>2</td>\n", |
| 114 | + " </tr>\n", |
| 115 | + " <tr>\n", |
| 116 | + " <th>4</th>\n", |
| 117 | + " <td>3</td>\n", |
| 118 | + " <td>7</td>\n", |
| 119 | + " <td>5</td>\n", |
| 120 | + " <td>8</td>\n", |
| 121 | + " </tr>\n", |
| 122 | + " <tr>\n", |
| 123 | + " <th rowspan=\"2\" valign=\"top\">C</th>\n", |
| 124 | + " <th>5</th>\n", |
| 125 | + " <td>2</td>\n", |
| 126 | + " <td>2</td>\n", |
| 127 | + " <td>0</td>\n", |
| 128 | + " <td>1</td>\n", |
| 129 | + " </tr>\n", |
| 130 | + " <tr>\n", |
| 131 | + " <th>6</th>\n", |
| 132 | + " <td>3</td>\n", |
| 133 | + " <td>6</td>\n", |
| 134 | + " <td>5</td>\n", |
| 135 | + " <td>4</td>\n", |
| 136 | + " </tr>\n", |
| 137 | + " <tr>\n", |
| 138 | + " <th rowspan=\"2\" valign=\"top\">D</th>\n", |
| 139 | + " <th>7</th>\n", |
| 140 | + " <td>7</td>\n", |
| 141 | + " <td>2</td>\n", |
| 142 | + " <td>5</td>\n", |
| 143 | + " <td>1</td>\n", |
| 144 | + " </tr>\n", |
| 145 | + " <tr>\n", |
| 146 | + " <th>8</th>\n", |
| 147 | + " <td>8</td>\n", |
| 148 | + " <td>5</td>\n", |
| 149 | + " <td>2</td>\n", |
| 150 | + " <td>5</td>\n", |
| 151 | + " </tr>\n", |
| 152 | + " </tbody>\n", |
| 153 | + "</table>\n", |
| 154 | + "</div>" |
| 155 | + ], |
| 156 | + "text/plain": [ |
| 157 | + " X1 X2 X3 X4\n", |
| 158 | + "I II \n", |
| 159 | + "A 1 1 8 2 2\n", |
| 160 | + " 2 6 5 2 3\n", |
| 161 | + "B 3 8 4 7 2\n", |
| 162 | + " 4 3 7 5 8\n", |
| 163 | + "C 5 2 2 0 1\n", |
| 164 | + " 6 3 6 5 4\n", |
| 165 | + "D 7 7 2 5 1\n", |
| 166 | + " 8 8 5 2 5" |
| 167 | + ] |
| 168 | + }, |
| 169 | + "execution_count": 4, |
| 170 | + "metadata": {}, |
| 171 | + "output_type": "execute_result" |
| 172 | + } |
| 173 | + ], |
| 174 | + "source": [ |
| 175 | + "# 创建DataFrame\n", |
| 176 | + "df = pd.DataFrame(data, \n", |
| 177 | + " index=row_idx, \n", |
| 178 | + " columns=['X1','X2','X3','X4'])\n", |
| 179 | + "df" |
| 180 | + ] |
| 181 | + } |
| 182 | + ], |
| 183 | + "metadata": { |
| 184 | + "kernelspec": { |
| 185 | + "display_name": "Python 3 (ipykernel)", |
| 186 | + "language": "python", |
| 187 | + "name": "python3" |
| 188 | + }, |
| 189 | + "language_info": { |
| 190 | + "codemirror_mode": { |
| 191 | + "name": "ipython", |
| 192 | + "version": 3 |
| 193 | + }, |
| 194 | + "file_extension": ".py", |
| 195 | + "mimetype": "text/x-python", |
| 196 | + "name": "python", |
| 197 | + "nbconvert_exporter": "python", |
| 198 | + "pygments_lexer": "ipython3", |
| 199 | + "version": "3.10.9" |
| 200 | + } |
| 201 | + }, |
| 202 | + "nbformat": 4, |
| 203 | + "nbformat_minor": 5 |
| 204 | +} |
0 commit comments