@@ -307,14 +307,13 @@ To evaluate single-element pandas objects in a boolean context, use the method
307
307
308
308
.. code-block :: python
309
309
310
- >> > if df: # noqa: E999
311
- ...
310
+ >> > if df: # noqa: E999
312
311
313
312
Or
314
313
315
314
.. code-block :: python
316
315
317
- >> > df and df2
316
+ >> > df and df2
318
317
319
318
These will both raise errors, as you are trying to compare multiple values.
320
319
@@ -330,17 +329,17 @@ Comparing if objects are equivalent
330
329
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
331
330
332
331
Often you may find that there is more than one way to compute the same
333
- result. As a simple example, consider ``df+ df `` and ``df* 2 ``. To test
332
+ result. As a simple example, consider ``df + df `` and ``df * 2 ``. To test
334
333
that these two computations produce the same result, given the tools
335
- shown above, you might imagine using ``(df+ df == df* 2).all() ``. But in
334
+ shown above, you might imagine using ``(df + df == df * 2).all() ``. But in
336
335
fact, this expression is False:
337
336
338
337
.. ipython :: python
339
338
340
339
df + df == df * 2
341
340
(df + df == df * 2 ).all()
342
341
343
- Notice that the boolean DataFrame ``df+ df == df* 2 `` contains some False values!
342
+ Notice that the boolean DataFrame ``df + df == df * 2 `` contains some False values!
344
343
This is because NaNs do not compare as equals:
345
344
346
345
.. ipython :: python
@@ -1506,14 +1505,15 @@ In short, basic iteration (``for i in object``) produces:
1506
1505
1507
1506
Thus, for example, iterating over a DataFrame gives you the column names:
1508
1507
1509
- .. ipython ::
1508
+ .. ipython :: python
1509
+
1510
+ df = pd.DataFrame(
1511
+ {' col1' : np.random.randn(3 ), ' col2' : np.random.randn(3 )},
1512
+ index = [' a' , ' b' , ' c' ])
1510
1513
1511
- In [0]: df = pd.DataFrame({'col1': np.random.randn(3), 'col2': np.random.randn(3)},
1512
- ...: index=['a', 'b', 'c'] )
1514
+ for col in df:
1515
+ print (col )
1513
1516
1514
- In [0]: for col in df:
1515
- ...: print(col)
1516
- ...:
1517
1517
1518
1518
Pandas objects also have the dict-like :meth: `~DataFrame.iteritems ` method to
1519
1519
iterate over the (key, value) pairs.
@@ -1576,12 +1576,11 @@ through key-value pairs:
1576
1576
1577
1577
For example:
1578
1578
1579
- .. ipython ::
1579
+ .. ipython :: python
1580
1580
1581
- In [0]: for item, frame in wp.iteritems():
1582
- ...: print(item)
1583
- ...: print(frame)
1584
- ...:
1581
+ for item, frame in wp.iteritems():
1582
+ print (item)
1583
+ print (frame)
1585
1584
1586
1585
.. _basics.iterrows :
1587
1586
@@ -1592,11 +1591,10 @@ iterrows
1592
1591
DataFrame as Series objects. It returns an iterator yielding each
1593
1592
index value along with a Series containing the data in each row:
1594
1593
1595
- .. ipython ::
1594
+ .. ipython :: python
1596
1595
1597
- In [0]: for row_index, row in df.iterrows():
1598
- ...: print('%s\n %s' % (row_index, row))
1599
- ...:
1596
+ for row_index, row in df.iterrows():
1597
+ print (' %s \n %s ' % (row_index, row))
1600
1598
1601
1599
.. note ::
1602
1600
@@ -1969,10 +1967,12 @@ with the data type of each column.
1969
1967
1970
1968
.. ipython :: python
1971
1969
1972
- dft = pd.DataFrame(dict (A = np.random.rand(3 ), B = 1 , C = ' foo' ,
1973
- D = pd.Timestamp(' 20010102' ),
1974
- E = pd.Series([1.0 ]* 3 ).astype(' float32' ),
1975
- F = False , G = pd.Series([1 ]* 3 ,dtype = ' int8' )))
1970
+ dft = pd.DataFrame(dict (A = np.random.rand(3 ),
1971
+ B = 1 ,
1972
+ C = ' foo' ,
1973
+ D = pd.Timestamp(' 20010102' ),
1974
+ E = pd.Series([1.0 ] * 3 ).astype(' float32' ),
1975
+ F = False , G = pd.Series([1 ] * 3 , dtype = ' int8' )))
1976
1976
dft
1977
1977
dft.dtypes
1978
1978
@@ -2011,10 +2011,10 @@ different numeric dtypes will **NOT** be combined. The following example will gi
2011
2011
df1 = pd.DataFrame(np.random.randn(8 , 1 ), columns = [' A' ], dtype = ' float32' )
2012
2012
df1
2013
2013
df1.dtypes
2014
- df2 = pd.DataFrame(dict (A = pd.Series(np.random.randn(8 ), dtype = ' float16' ),
2015
- B = pd.Series(np.random.randn(8 )),
2016
- C = pd.Series(np.array(
2017
- np.random.randn(8 ), dtype = ' uint8' )) ))
2014
+ df2 = pd.DataFrame(dict (A = pd.Series(np.random.randn(8 ), dtype = ' float16' ),
2015
+ B = pd.Series(np.random.randn(8 )),
2016
+ C = pd.Series(np.array(
2017
+ np.random.randn(8 ), dtype = ' uint8' ))))
2018
2018
df2
2019
2019
df2.dtypes
2020
2020
@@ -2029,7 +2029,7 @@ The following will all result in ``int64`` dtypes.
2029
2029
2030
2030
pd.DataFrame([1 , 2 ], columns = [' a' ]).dtypes
2031
2031
pd.DataFrame({' a' : [1 , 2 ]}).dtypes
2032
- pd.DataFrame({' a' : 1 }, index = list (range (2 ))).dtypes
2032
+ pd.DataFrame({' a' : 1 }, index = list (range (2 ))).dtypes
2033
2033
2034
2034
Note that Numpy will choose *platform-dependent * types when creating arrays.
2035
2035
The following **WILL ** result in ``int32 `` on 32-bit platform.
@@ -2084,8 +2084,8 @@ Convert a subset of columns to a specified type using :meth:`~DataFrame.astype`.
2084
2084
2085
2085
.. ipython :: python
2086
2086
2087
- dft = pd.DataFrame({' a' : [1 ,2 , 3 ], ' b' : [4 ,5 , 6 ], ' c' : [7 , 8 , 9 ]})
2088
- dft[[' a' ,' b' ]] = dft[[' a' ,' b' ]].astype(np.uint8)
2087
+ dft = pd.DataFrame({' a' : [1 , 2 , 3 ], ' b' : [4 , 5 , 6 ], ' c' : [7 , 8 , 9 ]})
2088
+ dft[[' a' , ' b' ]] = dft[[' a' , ' b' ]].astype(np.uint8)
2089
2089
dft
2090
2090
dft.dtypes
2091
2091
@@ -2095,7 +2095,7 @@ Convert certain columns to a specific dtype by passing a dict to :meth:`~DataFra
2095
2095
2096
2096
.. ipython :: python
2097
2097
2098
- dft1 = pd.DataFrame({' a' : [1 ,0 , 1 ], ' b' : [4 ,5 , 6 ], ' c' : [7 , 8 , 9 ]})
2098
+ dft1 = pd.DataFrame({' a' : [1 , 0 , 1 ], ' b' : [4 , 5 , 6 ], ' c' : [7 , 8 , 9 ]})
2099
2099
dft1 = dft1.astype({' a' : np.bool, ' c' : np.float64})
2100
2100
dft1
2101
2101
dft1.dtypes
@@ -2108,7 +2108,7 @@ Convert certain columns to a specific dtype by passing a dict to :meth:`~DataFra
2108
2108
2109
2109
.. ipython :: python
2110
2110
2111
- dft = pd.DataFrame({' a' : [1 ,2 , 3 ], ' b' : [4 ,5 , 6 ], ' c' : [7 , 8 , 9 ]})
2111
+ dft = pd.DataFrame({' a' : [1 , 2 , 3 ], ' b' : [4 , 5 , 6 ], ' c' : [7 , 8 , 9 ]})
2112
2112
dft.loc[:, [' a' , ' b' ]].astype(np.uint8).dtypes
2113
2113
dft.loc[:, [' a' , ' b' ]] = dft.loc[:, [' a' , ' b' ]].astype(np.uint8)
2114
2114
dft.dtypes
@@ -2244,7 +2244,7 @@ See also :ref:`Support for integer NA <gotchas.intna>`.
2244
2244
dfi
2245
2245
dfi.dtypes
2246
2246
2247
- casted = dfi[dfi> 0 ]
2247
+ casted = dfi[dfi > 0 ]
2248
2248
casted
2249
2249
casted.dtypes
2250
2250
@@ -2256,7 +2256,7 @@ While float dtypes are unchanged.
2256
2256
dfa[' A' ] = dfa[' A' ].astype(' float32' )
2257
2257
dfa.dtypes
2258
2258
2259
- casted = dfa[df2> 0 ]
2259
+ casted = dfa[df2 > 0 ]
2260
2260
casted
2261
2261
casted.dtypes
2262
2262
0 commit comments