|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "id": "f9b79835-51ef-4edc-86f7-047e36526942", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [ |
| 9 | + { |
| 10 | + "name": "stdout", |
| 11 | + "output_type": "stream", |
| 12 | + "text": [ |
| 13 | + "0 Jeff Russell\n", |
| 14 | + "1 Jane Boorman\n", |
| 15 | + "2 Tom Heints\n", |
| 16 | + "dtype: object\n" |
| 17 | + ] |
| 18 | + } |
| 19 | + ], |
| 20 | + "source": [ |
| 21 | + "import pandas as pd\n", |
| 22 | + "\n", |
| 23 | + "data = ['Jeff Russell', 'Jane Boorman', 'Tom Heints']\n", |
| 24 | + "emps_names = pd.Series(data)\n", |
| 25 | + "\n", |
| 26 | + "print(emps_names)" |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "code", |
| 31 | + "execution_count": 3, |
| 32 | + "id": "8b19bad6-f907-4881-835f-eeb1425f1e3f", |
| 33 | + "metadata": {}, |
| 34 | + "outputs": [ |
| 35 | + { |
| 36 | + "name": "stdout", |
| 37 | + "output_type": "stream", |
| 38 | + "text": [ |
| 39 | + "9001 Jeff Russell\n", |
| 40 | + "9002 Jane Boorman\n", |
| 41 | + "9003 Tom Heints\n", |
| 42 | + "dtype: object\n" |
| 43 | + ] |
| 44 | + } |
| 45 | + ], |
| 46 | + "source": [ |
| 47 | + "# Use custom indexes for series\n", |
| 48 | + "emps_names = pd.Series(data, index=[9001, 9002, 9003])\n", |
| 49 | + "print(emps_names)" |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "code", |
| 54 | + "execution_count": 4, |
| 55 | + "id": "18fd7336-3676-46f3-ba68-d2a3314cba75", |
| 56 | + "metadata": {}, |
| 57 | + "outputs": [ |
| 58 | + { |
| 59 | + "name": "stdout", |
| 60 | + "output_type": "stream", |
| 61 | + "text": [ |
| 62 | + "Jeff Russell\n" |
| 63 | + ] |
| 64 | + } |
| 65 | + ], |
| 66 | + "source": [ |
| 67 | + "# Access by Series Index\n", |
| 68 | + "emp_9001 = emps_names[9001]\n", |
| 69 | + "print(emp_9001)" |
| 70 | + ] |
| 71 | + }, |
| 72 | + { |
| 73 | + "cell_type": "code", |
| 74 | + "execution_count": 5, |
| 75 | + "id": "0335b0df-fca2-4a36-8101-e8b8144abaa1", |
| 76 | + "metadata": {}, |
| 77 | + "outputs": [ |
| 78 | + { |
| 79 | + "name": "stdout", |
| 80 | + "output_type": "stream", |
| 81 | + "text": [ |
| 82 | + "Jeff Russell\n" |
| 83 | + ] |
| 84 | + } |
| 85 | + ], |
| 86 | + "source": [ |
| 87 | + "# Access by Index\n", |
| 88 | + "emp_idx_0 = emps_names.iloc[0]\n", |
| 89 | + "print(emp_idx_0)" |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_type": "code", |
| 94 | + "execution_count": 7, |
| 95 | + "id": "09bc2f12-91a8-42f5-9b1b-be30e0c47d31", |
| 96 | + "metadata": {}, |
| 97 | + "outputs": [ |
| 98 | + { |
| 99 | + "name": "stdout", |
| 100 | + "output_type": "stream", |
| 101 | + "text": [ |
| 102 | + "9001 jeff.russel@company.com\n", |
| 103 | + "9002 jane.boorman@company.com\n", |
| 104 | + "9003 tom.heints@company.com\n", |
| 105 | + "dtype: object\n" |
| 106 | + ] |
| 107 | + } |
| 108 | + ], |
| 109 | + "source": [ |
| 110 | + "emails = ['jeff.russel@company.com', 'jane.boorman@company.com', 'tom.heints@company.com']\n", |
| 111 | + "emps_emails = pd.Series(emails, index=[9001, 9002, 9003])\n", |
| 112 | + "\n", |
| 113 | + "print(emps_emails)" |
| 114 | + ] |
| 115 | + }, |
| 116 | + { |
| 117 | + "cell_type": "code", |
| 118 | + "execution_count": 8, |
| 119 | + "id": "6437eb50-155e-41ec-8913-3772e027ac7b", |
| 120 | + "metadata": {}, |
| 121 | + "outputs": [ |
| 122 | + { |
| 123 | + "name": "stdout", |
| 124 | + "output_type": "stream", |
| 125 | + "text": [ |
| 126 | + " name email\n", |
| 127 | + "9001 Jeff Russell jeff.russel@company.com\n", |
| 128 | + "9002 Jane Boorman jane.boorman@company.com\n", |
| 129 | + "9003 Tom Heints tom.heints@company.com\n" |
| 130 | + ] |
| 131 | + } |
| 132 | + ], |
| 133 | + "source": [ |
| 134 | + "# Merge 2 Series into 1 turning both into a single Data Frame\n", |
| 135 | + "# Set a name for the existing series\n", |
| 136 | + "emps_names.name = 'name'\n", |
| 137 | + "emps_emails.name = 'email'\n", |
| 138 | + "\n", |
| 139 | + "# Concatenate using columns with `axis = 1`\n", |
| 140 | + "df = pd.concat([emps_names, emps_emails], axis = 1)\n", |
| 141 | + "\n", |
| 142 | + "print(df)" |
| 143 | + ] |
| 144 | + }, |
| 145 | + { |
| 146 | + "cell_type": "markdown", |
| 147 | + "id": "d77b56b9-5dd4-4338-be39-5f5fe11e5bc2", |
| 148 | + "metadata": {}, |
| 149 | + "source": [ |
| 150 | + "Create a three/four column DataFrame" |
| 151 | + ] |
| 152 | + }, |
| 153 | + { |
| 154 | + "cell_type": "code", |
| 155 | + "execution_count": 11, |
| 156 | + "id": "b64f2b29-734c-4563-98ce-5e0e6453ce35", |
| 157 | + "metadata": {}, |
| 158 | + "outputs": [ |
| 159 | + { |
| 160 | + "name": "stdout", |
| 161 | + "output_type": "stream", |
| 162 | + "text": [ |
| 163 | + "9001 +0 (982) 3454-8290\n", |
| 164 | + "9002 +0 (982) 1167-9388\n", |
| 165 | + "9003 +0 (982) 4544-8822\n", |
| 166 | + "Name: phone, dtype: object\n" |
| 167 | + ] |
| 168 | + } |
| 169 | + ], |
| 170 | + "source": [ |
| 171 | + "phones = ['+0 (982) 3454-8290', '+0 (982) 1167-9388', '+0 (982) 4544-8822']\n", |
| 172 | + "\n", |
| 173 | + "emps_phones = pd.Series(phones, index=[9001, 9002, 9003])\n", |
| 174 | + "emps_phones.name = 'phone'\n", |
| 175 | + "\n", |
| 176 | + "print(emps_phones)" |
| 177 | + ] |
| 178 | + }, |
| 179 | + { |
| 180 | + "cell_type": "code", |
| 181 | + "execution_count": 12, |
| 182 | + "id": "9c539e29-0c9d-49dc-a061-00822e409efe", |
| 183 | + "metadata": {}, |
| 184 | + "outputs": [ |
| 185 | + { |
| 186 | + "name": "stdout", |
| 187 | + "output_type": "stream", |
| 188 | + "text": [ |
| 189 | + "9001 Amsterdam\n", |
| 190 | + "9002 Budapest\n", |
| 191 | + "9003 Washington D.C.\n", |
| 192 | + "Name: location, dtype: object\n" |
| 193 | + ] |
| 194 | + } |
| 195 | + ], |
| 196 | + "source": [ |
| 197 | + "locations = ['Amsterdam', 'Budapest', 'Washington D.C.']\n", |
| 198 | + "\n", |
| 199 | + "emps_locations = pd.Series(locations, index=[9001, 9002, 9003])\n", |
| 200 | + "emps_locations.name = 'location'\n", |
| 201 | + "\n", |
| 202 | + "print(emps_locations)" |
| 203 | + ] |
| 204 | + }, |
| 205 | + { |
| 206 | + "cell_type": "code", |
| 207 | + "execution_count": 14, |
| 208 | + "id": "9da839b8-2fcf-46b2-941b-1689d76b7f8a", |
| 209 | + "metadata": {}, |
| 210 | + "outputs": [ |
| 211 | + { |
| 212 | + "name": "stdout", |
| 213 | + "output_type": "stream", |
| 214 | + "text": [ |
| 215 | + " name email phone \\\n", |
| 216 | + "9001 Jeff Russell jeff.russel@company.com +0 (982) 3454-8290 \n", |
| 217 | + "9002 Jane Boorman jane.boorman@company.com +0 (982) 1167-9388 \n", |
| 218 | + "9003 Tom Heints tom.heints@company.com +0 (982) 4544-8822 \n", |
| 219 | + "\n", |
| 220 | + " location \n", |
| 221 | + "9001 Amsterdam \n", |
| 222 | + "9002 Budapest \n", |
| 223 | + "9003 Washington D.C. \n" |
| 224 | + ] |
| 225 | + } |
| 226 | + ], |
| 227 | + "source": [ |
| 228 | + "df = pd.concat([emps_names, emps_emails, emps_phones, emps_locations], axis = 1)\n", |
| 229 | + "\n", |
| 230 | + "print(df)" |
| 231 | + ] |
| 232 | + } |
| 233 | + ], |
| 234 | + "metadata": { |
| 235 | + "kernelspec": { |
| 236 | + "display_name": "Python 3 (ipykernel)", |
| 237 | + "language": "python", |
| 238 | + "name": "python3" |
| 239 | + }, |
| 240 | + "language_info": { |
| 241 | + "codemirror_mode": { |
| 242 | + "name": "ipython", |
| 243 | + "version": 3 |
| 244 | + }, |
| 245 | + "file_extension": ".py", |
| 246 | + "mimetype": "text/x-python", |
| 247 | + "name": "python", |
| 248 | + "nbconvert_exporter": "python", |
| 249 | + "pygments_lexer": "ipython3", |
| 250 | + "version": "3.9.17" |
| 251 | + } |
| 252 | + }, |
| 253 | + "nbformat": 4, |
| 254 | + "nbformat_minor": 5 |
| 255 | +} |
0 commit comments