1
+ {
2
+ "metadata" : {
3
+ "name" : " " ,
4
+ "signature" : " sha256:895ef4622069632c7ca767372a14c9b64309d4c54fa940ae99c0662768a54ba6"
5
+ },
6
+ "nbformat" : 3 ,
7
+ "nbformat_minor" : 0 ,
8
+ "worksheets" : [
9
+ {
10
+ "cells" : [
11
+ {
12
+ "cell_type" : " markdown" ,
13
+ "metadata" : {},
14
+ "source" : [
15
+ " # Drop A Column That Contains A Certain String In Pandas\n " ,
16
+ " \n " ,
17
+ " This snippit was written by [Chris R. Albon](http://www.chrisralbon.com/) and is part of his collection of [well-documented Python snippits](https://github.com/chrisalbon/code_py). All code is written in Python 3 in iPython notebook and offered under the [Creative Commons Attribution-ShareAlike 4.0 International License](http://creativecommons.org/licenses/by-sa/4.0/)."
18
+ ]
19
+ },
20
+ {
21
+ "cell_type" : " code" ,
22
+ "collapsed" : false ,
23
+ "input" : [
24
+ " import pandas as pd"
25
+ ],
26
+ "language" : " python" ,
27
+ "metadata" : {},
28
+ "outputs" : [],
29
+ "prompt_number" : 3
30
+ },
31
+ {
32
+ "cell_type" : " code" ,
33
+ "collapsed" : false ,
34
+ "input" : [
35
+ " raw_data = {'regiment': ['Nighthawks', 'Nighthawks', 'Nighthawks', 'Nighthawks', 'Dragoons', 'Dragoons', 'Dragoons', 'Dragoons', 'Scouts', 'Scouts', 'Scouts', 'Scouts'], \n " ,
36
+ " 'company': ['1st', '1st', '2nd', '2nd', '1st', '1st', '2nd', '2nd','1st', '1st', '2nd', '2nd'], \n " ,
37
+ " 'name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze', 'Jacon', 'Ryaner', 'Sone', 'Sloan', 'Piger', 'Riani', 'Ali'], \n " ,
38
+ " 'preTestScore': [4, 24, 31, 2, 3, 4, 24, 31, 2, 3, 2, 3],\n " ,
39
+ " 'postTestScore': [25, 94, 57, 62, 70, 25, 94, 57, 62, 70, 62, 70]}\n " ,
40
+ " df = pd.DataFrame(raw_data, columns = ['regiment', 'company', 'name', 'preTestScore', 'postTestScore'])\n " ,
41
+ " df"
42
+ ],
43
+ "language" : " python" ,
44
+ "metadata" : {},
45
+ "outputs" : [
46
+ {
47
+ "html" : [
48
+ " <div style=\" max-height:1000px;max-width:1500px;overflow:auto;\" >\n " ,
49
+ " <table border=\" 1\" class=\" dataframe\" >\n " ,
50
+ " <thead>\n " ,
51
+ " <tr style=\" text-align: right;\" >\n " ,
52
+ " <th></th>\n " ,
53
+ " <th>regiment</th>\n " ,
54
+ " <th>company</th>\n " ,
55
+ " <th>name</th>\n " ,
56
+ " <th>preTestScore</th>\n " ,
57
+ " <th>postTestScore</th>\n " ,
58
+ " </tr>\n " ,
59
+ " </thead>\n " ,
60
+ " <tbody>\n " ,
61
+ " <tr>\n " ,
62
+ " <th>0 </th>\n " ,
63
+ " <td> Nighthawks</td>\n " ,
64
+ " <td> 1st</td>\n " ,
65
+ " <td> Miller</td>\n " ,
66
+ " <td> 4</td>\n " ,
67
+ " <td> 25</td>\n " ,
68
+ " </tr>\n " ,
69
+ " <tr>\n " ,
70
+ " <th>1 </th>\n " ,
71
+ " <td> Nighthawks</td>\n " ,
72
+ " <td> 1st</td>\n " ,
73
+ " <td> Jacobson</td>\n " ,
74
+ " <td> 24</td>\n " ,
75
+ " <td> 94</td>\n " ,
76
+ " </tr>\n " ,
77
+ " <tr>\n " ,
78
+ " <th>2 </th>\n " ,
79
+ " <td> Nighthawks</td>\n " ,
80
+ " <td> 2nd</td>\n " ,
81
+ " <td> Ali</td>\n " ,
82
+ " <td> 31</td>\n " ,
83
+ " <td> 57</td>\n " ,
84
+ " </tr>\n " ,
85
+ " <tr>\n " ,
86
+ " <th>3 </th>\n " ,
87
+ " <td> Nighthawks</td>\n " ,
88
+ " <td> 2nd</td>\n " ,
89
+ " <td> Milner</td>\n " ,
90
+ " <td> 2</td>\n " ,
91
+ " <td> 62</td>\n " ,
92
+ " </tr>\n " ,
93
+ " <tr>\n " ,
94
+ " <th>4 </th>\n " ,
95
+ " <td> Dragoons</td>\n " ,
96
+ " <td> 1st</td>\n " ,
97
+ " <td> Cooze</td>\n " ,
98
+ " <td> 3</td>\n " ,
99
+ " <td> 70</td>\n " ,
100
+ " </tr>\n " ,
101
+ " <tr>\n " ,
102
+ " <th>5 </th>\n " ,
103
+ " <td> Dragoons</td>\n " ,
104
+ " <td> 1st</td>\n " ,
105
+ " <td> Jacon</td>\n " ,
106
+ " <td> 4</td>\n " ,
107
+ " <td> 25</td>\n " ,
108
+ " </tr>\n " ,
109
+ " <tr>\n " ,
110
+ " <th>6 </th>\n " ,
111
+ " <td> Dragoons</td>\n " ,
112
+ " <td> 2nd</td>\n " ,
113
+ " <td> Ryaner</td>\n " ,
114
+ " <td> 24</td>\n " ,
115
+ " <td> 94</td>\n " ,
116
+ " </tr>\n " ,
117
+ " <tr>\n " ,
118
+ " <th>7 </th>\n " ,
119
+ " <td> Dragoons</td>\n " ,
120
+ " <td> 2nd</td>\n " ,
121
+ " <td> Sone</td>\n " ,
122
+ " <td> 31</td>\n " ,
123
+ " <td> 57</td>\n " ,
124
+ " </tr>\n " ,
125
+ " <tr>\n " ,
126
+ " <th>8 </th>\n " ,
127
+ " <td> Scouts</td>\n " ,
128
+ " <td> 1st</td>\n " ,
129
+ " <td> Sloan</td>\n " ,
130
+ " <td> 2</td>\n " ,
131
+ " <td> 62</td>\n " ,
132
+ " </tr>\n " ,
133
+ " <tr>\n " ,
134
+ " <th>9 </th>\n " ,
135
+ " <td> Scouts</td>\n " ,
136
+ " <td> 1st</td>\n " ,
137
+ " <td> Piger</td>\n " ,
138
+ " <td> 3</td>\n " ,
139
+ " <td> 70</td>\n " ,
140
+ " </tr>\n " ,
141
+ " <tr>\n " ,
142
+ " <th>10</th>\n " ,
143
+ " <td> Scouts</td>\n " ,
144
+ " <td> 2nd</td>\n " ,
145
+ " <td> Riani</td>\n " ,
146
+ " <td> 2</td>\n " ,
147
+ " <td> 62</td>\n " ,
148
+ " </tr>\n " ,
149
+ " <tr>\n " ,
150
+ " <th>11</th>\n " ,
151
+ " <td> Scouts</td>\n " ,
152
+ " <td> 2nd</td>\n " ,
153
+ " <td> Ali</td>\n " ,
154
+ " <td> 3</td>\n " ,
155
+ " <td> 70</td>\n " ,
156
+ " </tr>\n " ,
157
+ " </tbody>\n " ,
158
+ " </table>\n " ,
159
+ " </div>"
160
+ ],
161
+ "metadata" : {},
162
+ "output_type" : " pyout" ,
163
+ "prompt_number" : 4 ,
164
+ "text" : [
165
+ " regiment company name preTestScore postTestScore\n " ,
166
+ " 0 Nighthawks 1st Miller 4 25\n " ,
167
+ " 1 Nighthawks 1st Jacobson 24 94\n " ,
168
+ " 2 Nighthawks 2nd Ali 31 57\n " ,
169
+ " 3 Nighthawks 2nd Milner 2 62\n " ,
170
+ " 4 Dragoons 1st Cooze 3 70\n " ,
171
+ " 5 Dragoons 1st Jacon 4 25\n " ,
172
+ " 6 Dragoons 2nd Ryaner 24 94\n " ,
173
+ " 7 Dragoons 2nd Sone 31 57\n " ,
174
+ " 8 Scouts 1st Sloan 2 62\n " ,
175
+ " 9 Scouts 1st Piger 3 70\n " ,
176
+ " 10 Scouts 2nd Riani 2 62\n " ,
177
+ " 11 Scouts 2nd Ali 3 70"
178
+ ]
179
+ }
180
+ ],
181
+ "prompt_number" : 4
182
+ },
183
+ {
184
+ "cell_type" : " code" ,
185
+ "collapsed" : false ,
186
+ "input" : [
187
+ " # Create a variable that drop columns with column names where the first three letters of the column names was 'pre'\n " ,
188
+ " cols = [c for c in df.columns if c.lower()[:3] != 'pre']\n " ,
189
+ " \n " ,
190
+ " # Create a df of the columns in the variable cols\n " ,
191
+ " df=df[cols]"
192
+ ],
193
+ "language" : " python" ,
194
+ "metadata" : {},
195
+ "outputs" : [],
196
+ "prompt_number" : 11
197
+ },
198
+ {
199
+ "cell_type" : " code" ,
200
+ "collapsed" : false ,
201
+ "input" : [
202
+ " df"
203
+ ],
204
+ "language" : " python" ,
205
+ "metadata" : {},
206
+ "outputs" : [
207
+ {
208
+ "html" : [
209
+ " <div style=\" max-height:1000px;max-width:1500px;overflow:auto;\" >\n " ,
210
+ " <table border=\" 1\" class=\" dataframe\" >\n " ,
211
+ " <thead>\n " ,
212
+ " <tr style=\" text-align: right;\" >\n " ,
213
+ " <th></th>\n " ,
214
+ " <th>regiment</th>\n " ,
215
+ " <th>company</th>\n " ,
216
+ " <th>name</th>\n " ,
217
+ " <th>postTestScore</th>\n " ,
218
+ " </tr>\n " ,
219
+ " </thead>\n " ,
220
+ " <tbody>\n " ,
221
+ " <tr>\n " ,
222
+ " <th>0 </th>\n " ,
223
+ " <td> Nighthawks</td>\n " ,
224
+ " <td> 1st</td>\n " ,
225
+ " <td> Miller</td>\n " ,
226
+ " <td> 25</td>\n " ,
227
+ " </tr>\n " ,
228
+ " <tr>\n " ,
229
+ " <th>1 </th>\n " ,
230
+ " <td> Nighthawks</td>\n " ,
231
+ " <td> 1st</td>\n " ,
232
+ " <td> Jacobson</td>\n " ,
233
+ " <td> 94</td>\n " ,
234
+ " </tr>\n " ,
235
+ " <tr>\n " ,
236
+ " <th>2 </th>\n " ,
237
+ " <td> Nighthawks</td>\n " ,
238
+ " <td> 2nd</td>\n " ,
239
+ " <td> Ali</td>\n " ,
240
+ " <td> 57</td>\n " ,
241
+ " </tr>\n " ,
242
+ " <tr>\n " ,
243
+ " <th>3 </th>\n " ,
244
+ " <td> Nighthawks</td>\n " ,
245
+ " <td> 2nd</td>\n " ,
246
+ " <td> Milner</td>\n " ,
247
+ " <td> 62</td>\n " ,
248
+ " </tr>\n " ,
249
+ " <tr>\n " ,
250
+ " <th>4 </th>\n " ,
251
+ " <td> Dragoons</td>\n " ,
252
+ " <td> 1st</td>\n " ,
253
+ " <td> Cooze</td>\n " ,
254
+ " <td> 70</td>\n " ,
255
+ " </tr>\n " ,
256
+ " <tr>\n " ,
257
+ " <th>5 </th>\n " ,
258
+ " <td> Dragoons</td>\n " ,
259
+ " <td> 1st</td>\n " ,
260
+ " <td> Jacon</td>\n " ,
261
+ " <td> 25</td>\n " ,
262
+ " </tr>\n " ,
263
+ " <tr>\n " ,
264
+ " <th>6 </th>\n " ,
265
+ " <td> Dragoons</td>\n " ,
266
+ " <td> 2nd</td>\n " ,
267
+ " <td> Ryaner</td>\n " ,
268
+ " <td> 94</td>\n " ,
269
+ " </tr>\n " ,
270
+ " <tr>\n " ,
271
+ " <th>7 </th>\n " ,
272
+ " <td> Dragoons</td>\n " ,
273
+ " <td> 2nd</td>\n " ,
274
+ " <td> Sone</td>\n " ,
275
+ " <td> 57</td>\n " ,
276
+ " </tr>\n " ,
277
+ " <tr>\n " ,
278
+ " <th>8 </th>\n " ,
279
+ " <td> Scouts</td>\n " ,
280
+ " <td> 1st</td>\n " ,
281
+ " <td> Sloan</td>\n " ,
282
+ " <td> 62</td>\n " ,
283
+ " </tr>\n " ,
284
+ " <tr>\n " ,
285
+ " <th>9 </th>\n " ,
286
+ " <td> Scouts</td>\n " ,
287
+ " <td> 1st</td>\n " ,
288
+ " <td> Piger</td>\n " ,
289
+ " <td> 70</td>\n " ,
290
+ " </tr>\n " ,
291
+ " <tr>\n " ,
292
+ " <th>10</th>\n " ,
293
+ " <td> Scouts</td>\n " ,
294
+ " <td> 2nd</td>\n " ,
295
+ " <td> Riani</td>\n " ,
296
+ " <td> 62</td>\n " ,
297
+ " </tr>\n " ,
298
+ " <tr>\n " ,
299
+ " <th>11</th>\n " ,
300
+ " <td> Scouts</td>\n " ,
301
+ " <td> 2nd</td>\n " ,
302
+ " <td> Ali</td>\n " ,
303
+ " <td> 70</td>\n " ,
304
+ " </tr>\n " ,
305
+ " </tbody>\n " ,
306
+ " </table>\n " ,
307
+ " </div>"
308
+ ],
309
+ "metadata" : {},
310
+ "output_type" : " pyout" ,
311
+ "prompt_number" : 12 ,
312
+ "text" : [
313
+ " regiment company name postTestScore\n " ,
314
+ " 0 Nighthawks 1st Miller 25\n " ,
315
+ " 1 Nighthawks 1st Jacobson 94\n " ,
316
+ " 2 Nighthawks 2nd Ali 57\n " ,
317
+ " 3 Nighthawks 2nd Milner 62\n " ,
318
+ " 4 Dragoons 1st Cooze 70\n " ,
319
+ " 5 Dragoons 1st Jacon 25\n " ,
320
+ " 6 Dragoons 2nd Ryaner 94\n " ,
321
+ " 7 Dragoons 2nd Sone 57\n " ,
322
+ " 8 Scouts 1st Sloan 62\n " ,
323
+ " 9 Scouts 1st Piger 70\n " ,
324
+ " 10 Scouts 2nd Riani 62\n " ,
325
+ " 11 Scouts 2nd Ali 70"
326
+ ]
327
+ }
328
+ ],
329
+ "prompt_number" : 12
330
+ },
331
+ {
332
+ "cell_type" : " code" ,
333
+ "collapsed" : false ,
334
+ "input" : [],
335
+ "language" : " python" ,
336
+ "metadata" : {},
337
+ "outputs" : []
338
+ }
339
+ ],
340
+ "metadata" : {}
341
+ }
342
+ ]
343
+ }
0 commit comments