52
52
These are some neat pandas ``idioms ``
53
53
54
54
`if-then/if-then-else on one column, and assignment to another one or more columns:
55
- <http ://stackoverflow.com/questions/17128302/python-pandas-idiom-for-if-then-else> `__
55
+ <https ://stackoverflow.com/questions/17128302/python-pandas-idiom-for-if-then-else> `__
56
56
57
57
.. ipython :: python
58
58
@@ -88,7 +88,7 @@ Or use pandas where after you've set up a mask
88
88
df.where(df_mask,- 1000 )
89
89
90
90
`if-then-else using numpy's where()
91
- <http ://stackoverflow.com/questions/19913659/pandas-conditional-creation-of-a-series-dataframe-column> `__
91
+ <https ://stackoverflow.com/questions/19913659/pandas-conditional-creation-of-a-series-dataframe-column> `__
92
92
93
93
.. ipython :: python
94
94
@@ -101,7 +101,7 @@ Splitting
101
101
*********
102
102
103
103
`Split a frame with a boolean criterion
104
- <http ://stackoverflow.com/questions/14957116/how-to-split-a-dataframe-according-to-a-boolean-criterion> `__
104
+ <https ://stackoverflow.com/questions/14957116/how-to-split-a-dataframe-according-to-a-boolean-criterion> `__
105
105
106
106
.. ipython :: python
107
107
@@ -115,7 +115,7 @@ Building Criteria
115
115
*****************
116
116
117
117
`Select with multi-column criteria
118
- <http ://stackoverflow.com/questions/15315452/selecting-with-complex-criteria-from-pandas-dataframe> `__
118
+ <https ://stackoverflow.com/questions/15315452/selecting-with-complex-criteria-from-pandas-dataframe> `__
119
119
120
120
.. ipython :: python
121
121
@@ -141,7 +141,7 @@ Building Criteria
141
141
df.loc[(df[' BBB' ] > 25 ) | (df[' CCC' ] >= 75 ), ' AAA' ] = 0.1 ; df
142
142
143
143
`Select rows with data closest to certain value using argsort
144
- <http ://stackoverflow.com/questions/17758023/return-rows-in-a-dataframe-closest-to-a-user-defined-number> `__
144
+ <https ://stackoverflow.com/questions/17758023/return-rows-in-a-dataframe-closest-to-a-user-defined-number> `__
145
145
146
146
.. ipython :: python
147
147
@@ -152,7 +152,7 @@ Building Criteria
152
152
df.loc[(df.CCC - aValue).abs().argsort()]
153
153
154
154
`Dynamically reduce a list of criteria using a binary operators
155
- <http ://stackoverflow.com/questions/21058254/pandas-boolean-operation-in-a-python-list/21058331> `__
155
+ <https ://stackoverflow.com/questions/21058254/pandas-boolean-operation-in-a-python-list/21058331> `__
156
156
157
157
.. ipython :: python
158
158
@@ -189,7 +189,7 @@ DataFrames
189
189
The :ref: `indexing <indexing >` docs.
190
190
191
191
`Using both row labels and value conditionals
192
- <http ://stackoverflow.com/questions/14725068/pandas-using-row-labels-in-boolean-indexing> `__
192
+ <https ://stackoverflow.com/questions/14725068/pandas-using-row-labels-in-boolean-indexing> `__
193
193
194
194
.. ipython :: python
195
195
@@ -232,7 +232,7 @@ Ambiguity arises when an index consists of integers with a non-zero start or non
232
232
df2.loc[1 :3 ] # Label-oriented
233
233
234
234
`Using inverse operator (~) to take the complement of a mask
235
- <http ://stackoverflow.com/questions/14986510/picking-out-elements-based-on-complement-of-indices-in-python-pandas> `__
235
+ <https ://stackoverflow.com/questions/14986510/picking-out-elements-based-on-complement-of-indices-in-python-pandas> `__
236
236
237
237
.. ipython :: python
238
238
@@ -259,13 +259,13 @@ Panels
259
259
pf.loc[:,:,' F' ] = pd.DataFrame(data, rng, cols);pf
260
260
261
261
`Mask a panel by using np.where and then reconstructing the panel with the new masked values
262
- <http ://stackoverflow.com/questions/14650341/boolean-mask-in-pandas-panel> `__
262
+ <https ://stackoverflow.com/questions/14650341/boolean-mask-in-pandas-panel> `__
263
263
264
264
New Columns
265
265
***********
266
266
267
267
`Efficiently and dynamically creating new columns using applymap
268
- <http ://stackoverflow.com/questions/16575868/efficiently-creating-additional-columns-in-a-pandas-dataframe-using-map> `__
268
+ <https ://stackoverflow.com/questions/16575868/efficiently-creating-additional-columns-in-a-pandas-dataframe-using-map> `__
269
269
270
270
.. ipython :: python
271
271
@@ -279,7 +279,7 @@ New Columns
279
279
df[new_cols] = df[source_cols].applymap(categories.get);df
280
280
281
281
`Keep other columns when using min() with groupby
282
- <http ://stackoverflow.com/questions/23394476/keep-other-columns-when-using-min-with-groupby> `__
282
+ <https ://stackoverflow.com/questions/23394476/keep-other-columns-when-using-min-with-groupby> `__
283
283
284
284
.. ipython :: python
285
285
@@ -308,7 +308,7 @@ MultiIndexing
308
308
The :ref: `multindexing <advanced.hierarchical >` docs.
309
309
310
310
`Creating a MultiIndex from a labeled frame
311
- <http ://stackoverflow.com/questions/14916358/reshaping-dataframes-in-pandas-based-on-column-labels> `__
311
+ <https ://stackoverflow.com/questions/14916358/reshaping-dataframes-in-pandas-based-on-column-labels> `__
312
312
313
313
.. ipython :: python
314
314
@@ -331,7 +331,7 @@ Arithmetic
331
331
**********
332
332
333
333
`Performing arithmetic with a MultiIndex that needs broadcasting
334
- <http ://stackoverflow.com/questions/19501510/divide-entire-pandas-multiindex-dataframe-by-dataframe-variable/19502176#19502176> `__
334
+ <https ://stackoverflow.com/questions/19501510/divide-entire-pandas-multiindex-dataframe-by-dataframe-variable/19502176#19502176> `__
335
335
336
336
.. ipython :: python
337
337
@@ -343,7 +343,7 @@ Slicing
343
343
*******
344
344
345
345
`Slicing a MultiIndex with xs
346
- <http ://stackoverflow.com/questions/12590131/how-to-slice-multindex-columns-in-pandas-dataframes> `__
346
+ <https ://stackoverflow.com/questions/12590131/how-to-slice-multindex-columns-in-pandas-dataframes> `__
347
347
348
348
.. ipython :: python
349
349
@@ -364,7 +364,7 @@ To take the cross section of the 1st level and 1st axis the index:
364
364
df.xs(' six' ,level = 1 ,axis = 0 )
365
365
366
366
`Slicing a MultiIndex with xs, method #2
367
- <http ://stackoverflow.com/questions/14964493/multiindex-based-indexing-in-pandas> `__
367
+ <https ://stackoverflow.com/questions/14964493/multiindex-based-indexing-in-pandas> `__
368
368
369
369
.. ipython :: python
370
370
@@ -387,13 +387,13 @@ To take the cross section of the 1st level and 1st axis the index:
387
387
df.loc[(All,' Math' ),(All,' II' )]
388
388
389
389
`Setting portions of a MultiIndex with xs
390
- <http ://stackoverflow.com/questions/19319432/pandas-selecting-a-lower-level-in-a-dataframe-to-do-a-ffill> `__
390
+ <https ://stackoverflow.com/questions/19319432/pandas-selecting-a-lower-level-in-a-dataframe-to-do-a-ffill> `__
391
391
392
392
Sorting
393
393
*******
394
394
395
395
`Sort by specific column or an ordered list of columns, with a MultiIndex
396
- <http ://stackoverflow.com/questions/14733871/mutli-index-sorting-in-pandas> `__
396
+ <https ://stackoverflow.com/questions/14733871/mutli-index-sorting-in-pandas> `__
397
397
398
398
.. ipython :: python
399
399
0 commit comments