|
11 | 11 | "cell_type": "markdown",
|
12 | 12 | "metadata": {},
|
13 | 13 | "source": [
|
14 |
| - "Copied from [https://github.com/jakevdp/PythonDataScienceHandbook](https://github.com/jakevdp/PythonDataScienceHandbook)" |
| 14 | + "Copied from [https://github.com/jakevdp/PythonDataScienceHandbook](https://github.com/jakevdp/PythonDataScienceHandbook) with modifications to demonstrate notebook diffing." |
15 | 15 | ]
|
16 | 16 | },
|
17 | 17 | {
|
18 | 18 | "cell_type": "markdown",
|
19 | 19 | "metadata": {},
|
20 | 20 | "source": [
|
21 | 21 | "One of the essential pieces of NumPy is the ability to perform quick element-wise operations, both with basic arithmetic (addition, subtraction, multiplication, etc.) and with more sophisticated operations (trigonometric functions, exponential and logarithmic functions, etc.).\n",
|
22 |
| - "Pandas inherits much of this functionality from NumPy, and the ufuncs that we introduced in [Computation on NumPy Arrays: Universal Functions](02.03-Computation-on-arrays-ufuncs.ipynb) are key to this.\n", |
| 22 | + "Pandas inherits much of this functionality from NumPy, and the ufuncs that we introduced in [Computation on NumPy Arrays: Universal Functions](https://gitnotebooks.com/blog) are key to this.\n", |
23 | 23 | "\n",
|
24 | 24 | "Pandas includes a couple useful twists, however: for unary operations like negation and trigonometric functions, these ufuncs will *preserve index and column labels* in the output, and for binary operations such as addition and multiplication, Pandas will automatically *align indices* when passing the objects to the ufunc.\n",
|
25 | 25 | "This means that keeping the context of data and combining data from different sources–both potentially error-prone tasks with raw NumPy arrays–become essentially foolproof ones with Pandas.\n",
|
|
28 | 28 | },
|
29 | 29 | {
|
30 | 30 | "cell_type": "code",
|
31 |
| - "execution_count": 121, |
| 31 | + "execution_count": 26, |
32 | 32 | "metadata": {
|
33 | 33 | "collapsed": true
|
34 | 34 | },
|
|
48 | 48 | },
|
49 | 49 | {
|
50 | 50 | "cell_type": "code",
|
51 |
| - "execution_count": 122, |
| 51 | + "execution_count": 27, |
52 | 52 | "metadata": {
|
53 | 53 | "collapsed": false
|
54 | 54 | },
|
|
68 | 68 | },
|
69 | 69 | {
|
70 | 70 | "cell_type": "code",
|
71 |
| - "execution_count": 123, |
| 71 | + "execution_count": 28, |
72 | 72 | "metadata": {
|
73 | 73 | "collapsed": false
|
74 | 74 | },
|
|
77 | 77 | "data": {
|
78 | 78 | "text/plain": [
|
79 | 79 | "0 2.0\n",
|
80 |
| - "1 5.0\n", |
81 |
| - "2 9.0\n", |
82 |
| - "3 5.0\n", |
| 80 | + "1 3.0\n", |
| 81 | + "2 3.0\n", |
| 82 | + "3 -5.0\n", |
83 | 83 | "dtype: float64"
|
84 | 84 | ]
|
85 | 85 | },
|
86 |
| - "execution_count": 123, |
| 86 | + "execution_count": 28, |
87 | 87 | "metadata": {},
|
88 | 88 | "output_type": "execute_result"
|
89 | 89 | }
|
90 | 90 | ],
|
91 | 91 | "source": [
|
92 |
| - "A.add(B, fill_value=0)" |
| 92 | + "A.subtract(B, fill_value=0)" |
93 | 93 | ]
|
94 | 94 | },
|
95 | 95 | {
|
96 | 96 | "cell_type": "markdown",
|
97 | 97 | "metadata": {},
|
98 | 98 | "source": [
|
99 |
| - "Notice that indices are aligned correctly irrespective of their order in the two objects, and indices in the result are sorted.\n", |
| 99 | + "Observe that the indices align accurately regardless of their sequence in the two objects, and the result's indices are organized in ascending order.\n", |
100 | 100 | "As was the case with ``Series``, we can use the associated object's arithmetic method and pass any desired ``fill_value`` to be used in place of missing entries.\n",
|
101 | 101 | "Here we'll fill with the mean of all values in ``A`` (computed by first stacking the rows of ``A``):"
|
102 | 102 | ]
|
|
144 | 144 | " <th></th>\n",
|
145 | 145 | " <th>A</th>\n",
|
146 | 146 | " <th>B</th>\n",
|
147 |
| - " <th>C</th>\n", |
148 | 147 | " </tr>\n",
|
149 | 148 | " </thead>\n",
|
150 | 149 | " <tbody>\n",
|
151 | 150 | " <tr>\n",
|
152 | 151 | " <th>0</th>\n",
|
153 | 152 | " <td>19.00</td>\n",
|
154 |
| - " <td>20.00</td>\n", |
155 |
| - " <td>16.75</td>\n", |
| 153 | + " <td>26.0</td>\n", |
156 | 154 | " </tr>\n",
|
157 | 155 | " <tr>\n",
|
158 | 156 | " <th>1</th>\n",
|
159 | 157 | " <td>8.00</td>\n",
|
160 |
| - " <td>3.00</td>\n", |
161 |
| - " <td>12.75</td>\n", |
| 158 | + " <td>19.0</td>\n", |
162 | 159 | " </tr>\n",
|
163 | 160 | " <tr>\n",
|
164 | 161 | " <th>2</th>\n",
|
165 |
| - " <td>16.75</td>\n", |
166 |
| - " <td>10.75</td>\n", |
167 |
| - " <td>12.75</td>\n", |
| 162 | + " <td>53.0</td>\n", |
| 163 | + " <td>56.0</td>\n", |
168 | 164 | " </tr>\n",
|
169 | 165 | " </tbody>\n",
|
170 | 166 | "</table>\n",
|
171 | 167 | "</div>"
|
172 | 168 | ],
|
173 | 169 | "text/plain": [
|
174 |
| - " A B C\n", |
175 |
| - "0 19.00 20.00 16.75\n", |
176 |
| - "1 8.00 3.00 12.75\n", |
177 |
| - "2 16.75 10.75 12.75" |
| 170 | + " A B C\n", |
| 171 | + "0 10.0 26.0 55.0\n", |
| 172 | + "1 16.0 19.0 55.0\n", |
| 173 | + "2 53.0 56.0 52.0" |
178 | 174 | ]
|
179 | 175 | },
|
180 |
| - "execution_count": 127, |
| 176 | + "execution_count": 30, |
181 | 177 | "metadata": {},
|
182 | 178 | "output_type": "execute_result"
|
183 | 179 | }
|
|
200 | 196 | "# Large cells? No problem. Cells are collapsed to showcase the diff\n",
|
201 | 197 | "# Large cells? No problem. Cells are collapsed to showcase the diff\n",
|
202 | 198 | "\n",
|
203 |
| - "fill = A.stack().mean()\n", |
| 199 | + "fill = A.stack().sum()\n", |
204 | 200 | "A.add(B, fill_value=fill)\n",
|
205 | 201 | "\n",
|
206 | 202 | "# Large cells? No problem. Cells are collapsed to showcase the diff\n",
|
|
225 | 221 | "cell_type": "markdown",
|
226 | 222 | "metadata": {},
|
227 | 223 | "source": [
|
228 |
| - "## Ufuncs: Operations Between DataFrame and Series\n", |
| 224 | + "## Ufuncs: Operations Between DataFrame and Series with a changed header\n", |
229 | 225 | "\n",
|
230 | 226 | "When performing operations between a ``DataFrame`` and a ``Series``, the index and column alignment is similarly maintained.\n",
|
231 | 227 | "Operations between a ``DataFrame`` and a ``Series`` are similar to operations between a two-dimensional and one-dimensional NumPy array.\n",
|
|
234 | 230 | },
|
235 | 231 | {
|
236 | 232 | "cell_type": "code",
|
237 |
| - "execution_count": 128, |
| 233 | + "execution_count": 31, |
238 | 234 | "metadata": {
|
239 | 235 | "collapsed": false
|
240 | 236 | },
|
241 | 237 | "outputs": [
|
242 | 238 | {
|
243 | 239 | "data": {
|
244 | 240 | "text/plain": [
|
245 |
| - "array([[1, 5, 5, 9],\n", |
246 |
| - " [3, 5, 1, 9],\n", |
247 |
| - " [1, 9, 3, 7]])" |
| 241 | + "array([[7, 7, 2, 5],\n", |
| 242 | + " [4, 1, 7, 5],\n", |
| 243 | + " [1, 4, 0, 9]])" |
248 | 244 | ]
|
249 | 245 | },
|
250 |
| - "execution_count": 128, |
| 246 | + "execution_count": 31, |
251 | 247 | "metadata": {},
|
252 | 248 | "output_type": "execute_result"
|
253 | 249 | }
|
|
259 | 255 | },
|
260 | 256 | {
|
261 | 257 | "cell_type": "code",
|
262 |
| - "execution_count": 129, |
| 258 | + "execution_count": 32, |
263 | 259 | "metadata": {
|
264 | 260 | "collapsed": false
|
265 | 261 | },
|
|
268 | 264 | "data": {
|
269 | 265 | "text/plain": [
|
270 | 266 | "array([[ 0, 0, 0, 0],\n",
|
271 |
| - " [ 2, 0, -4, 0],\n", |
272 |
| - " [ 0, 4, -2, -2]])" |
| 267 | + " [-3, -6, 5, 0],\n", |
| 268 | + " [-6, -3, -2, 4]])" |
273 | 269 | ]
|
274 | 270 | },
|
275 |
| - "execution_count": 129, |
| 271 | + "execution_count": 32, |
276 | 272 | "metadata": {},
|
277 | 273 | "output_type": "execute_result"
|
278 | 274 | }
|
|
0 commit comments