@@ -71,7 +71,7 @@ Series can be instantiated from dicts:
71
71
72
72
.. ipython :: python
73
73
74
- d = {' b' : 1 , ' a' : 0 , ' c' : 2 }
74
+ d = {' b' : 1 , ' a' : 0 , ' c' : 2 }
75
75
pd.Series(d)
76
76
77
77
.. note ::
@@ -92,7 +92,7 @@ index will be pulled out.
92
92
93
93
.. ipython :: python
94
94
95
- d = {' a' : 0 ., ' b' : 1 ., ' c' : 2 .}
95
+ d = {' a' : 0 ., ' b' : 1 ., ' c' : 2 .}
96
96
pd.Series(d)
97
97
pd.Series(d, index = [' b' , ' c' , ' d' , ' a' ])
98
98
@@ -304,8 +304,8 @@ keys.
304
304
305
305
.. ipython :: python
306
306
307
- d = {' one' : pd.Series([1 ., 2 ., 3 .], index = [' a' , ' b' , ' c' ]),
308
- ' two' : pd.Series([1 ., 2 ., 3 ., 4 .], index = [' a' , ' b' , ' c' , ' d' ])}
307
+ d = {' one' : pd.Series([1 ., 2 ., 3 .], index = [' a' , ' b' , ' c' ]),
308
+ ' two' : pd.Series([1 ., 2 ., 3 ., 4 .], index = [' a' , ' b' , ' c' , ' d' ])}
309
309
df = pd.DataFrame(d)
310
310
df
311
311
@@ -334,8 +334,8 @@ result will be ``range(n)``, where ``n`` is the array length.
334
334
335
335
.. ipython :: python
336
336
337
- d = {' one' : [1 ., 2 ., 3 ., 4 .],
338
- ' two' : [4 ., 3 ., 2 ., 1 .]}
337
+ d = {' one' : [1 ., 2 ., 3 ., 4 .],
338
+ ' two' : [4 ., 3 ., 2 ., 1 .]}
339
339
pd.DataFrame(d)
340
340
pd.DataFrame(d, index = [' a' , ' b' , ' c' , ' d' ])
341
341
@@ -346,8 +346,8 @@ This case is handled identically to a dict of arrays.
346
346
347
347
.. ipython :: python
348
348
349
- data = np.zeros((2 ,), dtype = [(' A' , ' i4' ),(' B' , ' f4' ),(' C' , ' a10' )])
350
- data[:] = [(1 ,2 .,' Hello' ), (2 ,3 .," World" )]
349
+ data = np.zeros((2 , ), dtype = [(' A' , ' i4' ), (' B' , ' f4' ), (' C' , ' a10' )])
350
+ data[:] = [(1 , 2 ., ' Hello' ), (2 , 3 ., " World" )]
351
351
352
352
pd.DataFrame(data)
353
353
pd.DataFrame(data, index = [' first' , ' second' ])
@@ -507,17 +507,15 @@ derived from existing columns.
507
507
508
508
iris = pd.read_csv(' data/iris.data' )
509
509
iris.head()
510
-
511
- (iris.assign(sepal_ratio = iris[' SepalWidth' ] / iris[' SepalLength' ])
510
+ (iris.assign(sepal_ratio = iris[' SepalWidth' ] / iris[' SepalLength' ])
512
511
.head())
513
512
514
513
In the example above, we inserted a precomputed value. We can also pass in
515
514
a function of one argument to be evaluated on the DataFrame being assigned to.
516
515
517
516
.. ipython :: python
518
517
519
- iris.assign(sepal_ratio = lambda x : (x[' SepalWidth' ] /
520
- x[' SepalLength' ])).head()
518
+ iris.assign(sepal_ratio = lambda x : (x[' SepalWidth' ] / x[' SepalLength' ])).head()
521
519
522
520
``assign `` **always ** returns a copy of the data, leaving the original
523
521
DataFrame untouched.
@@ -532,8 +530,8 @@ greater than 5, calculate the ratio, and plot:
532
530
533
531
@savefig basics_assign.png
534
532
(iris.query(' SepalLength > 5' )
535
- .assign(SepalRatio = lambda x : x.SepalWidth / x.SepalLength,
536
- PetalRatio = lambda x : x.PetalWidth / x.PetalLength)
533
+ .assign(SepalRatio = lambda x : x.SepalWidth / x.SepalLength,
534
+ PetalRatio = lambda x : x.PetalWidth / x.PetalLength)
537
535
.plot(kind = ' scatter' , x = ' SepalRatio' , y = ' PetalRatio' ))
538
536
539
537
Since a function is passed in, the function is computed on the DataFrame
@@ -705,8 +703,8 @@ Boolean operators work as well:
705
703
706
704
.. ipython :: python
707
705
708
- df1 = pd.DataFrame({' a' : [1 , 0 , 1 ], ' b' : [0 , 1 , 1 ] }, dtype = bool )
709
- df2 = pd.DataFrame({' a' : [0 , 1 , 1 ], ' b' : [1 , 1 , 0 ] }, dtype = bool )
706
+ df1 = pd.DataFrame({' a' : [1 , 0 , 1 ], ' b' : [0 , 1 , 1 ]}, dtype = bool )
707
+ df2 = pd.DataFrame({' a' : [0 , 1 , 1 ], ' b' : [1 , 1 , 0 ]}, dtype = bool )
710
708
df1 & df2
711
709
df1 | df2
712
710
df1 ^ df2
@@ -746,7 +744,7 @@ Similarly, the dot method on Series implements dot product:
746
744
747
745
.. ipython :: python
748
746
749
- s1 = pd.Series(np.arange(5 ,10 ))
747
+ s1 = pd.Series(np.arange(5 , 10 ))
750
748
s1.dot(s1)
751
749
752
750
DataFrame is not intended to be a drop-in replacement for ndarray as its
@@ -777,7 +775,7 @@ R package):
777
775
:okwarning:
778
776
779
777
# restore GlobalPrintConfig
780
- pd.reset_option(' ^display\.' )
778
+ pd.reset_option(r ' ^ display\. ' )
781
779
782
780
However, using ``to_string `` will return a string representation of the
783
781
DataFrame in tabular form, though it won't always fit the console width:
@@ -798,22 +796,22 @@ option:
798
796
799
797
.. ipython :: python
800
798
801
- pd.set_option(' display.width' , 40 ) # default is 80
799
+ pd.set_option(' display.width' , 40 ) # default is 80
802
800
803
801
pd.DataFrame(np.random.randn(3 , 12 ))
804
802
805
803
You can adjust the max width of the individual columns by setting ``display.max_colwidth ``
806
804
807
805
.. ipython :: python
808
806
809
- datafile= {' filename' : [' filename_01' ,' filename_02' ],
810
- ' path' : [" media/user_name/storage/folder_01/filename_01" ,
811
- " media/user_name/storage/folder_02/filename_02" ]}
807
+ datafile = {' filename' : [' filename_01' , ' filename_02' ],
808
+ ' path' : [" media/user_name/storage/folder_01/filename_01" ,
809
+ " media/user_name/storage/folder_02/filename_02" ]}
812
810
813
- pd.set_option(' display.max_colwidth' ,30 )
811
+ pd.set_option(' display.max_colwidth' , 30 )
814
812
pd.DataFrame(datafile)
815
813
816
- pd.set_option(' display.max_colwidth' ,100 )
814
+ pd.set_option(' display.max_colwidth' , 100 )
817
815
pd.DataFrame(datafile)
818
816
819
817
.. ipython :: python
@@ -833,8 +831,8 @@ accessed like an attribute:
833
831
834
832
.. ipython :: python
835
833
836
- df = pd.DataFrame({' foo1' : np.random.randn(5 ),
837
- ' foo2' : np.random.randn(5 )})
834
+ df = pd.DataFrame({' foo1' : np.random.randn(5 ),
835
+ ' foo2' : np.random.randn(5 )})
838
836
df
839
837
df.foo1
840
838
@@ -843,7 +841,7 @@ completion mechanism so they can be tab-completed:
843
841
844
842
.. code-block :: ipython
845
843
846
- In [5]: df.fo<TAB>
844
+ In [5]: df.fo<TAB> # noqa: E225, E999
847
845
df.foo1 df.foo2
848
846
849
847
.. _basics.panel :
@@ -890,8 +888,8 @@ From dict of DataFrame objects
890
888
.. ipython :: python
891
889
:okwarning:
892
890
893
- data = {' Item1' : pd.DataFrame(np.random.randn(4 , 3 )),
894
- ' Item2' : pd.DataFrame(np.random.randn(4 , 2 ))}
891
+ data = {' Item1' : pd.DataFrame(np.random.randn(4 , 3 )),
892
+ ' Item2' : pd.DataFrame(np.random.randn(4 , 2 ))}
895
893
pd.Panel(data)
896
894
897
895
Note that the values in the dict need only be **convertible to
@@ -947,8 +945,9 @@ From DataFrame using ``to_panel`` method
947
945
.. ipython :: python
948
946
:okwarning:
949
947
950
- midx = pd.MultiIndex(levels = [[' one' , ' two' ], [' x' ,' y' ]], codes = [[1 ,1 ,0 ,0 ],[1 ,0 ,1 ,0 ]])
951
- df = pd.DataFrame({' A' : [1 , 2 , 3 , 4 ], ' B' : [5 , 6 , 7 , 8 ]}, index = midx)
948
+ midx = pd.MultiIndex(levels = [[' one' , ' two' ], [' x' , ' y' ]],
949
+ codes = [[1 , 1 , 0 , 0 ], [1 , 0 , 1 , 0 ]])
950
+ df = pd.DataFrame({' A' : [1 , 2 , 3 , 4 ], ' B' : [5 , 6 , 7 , 8 ]}, index = midx)
952
951
df.to_panel()
953
952
954
953
.. _dsintro.panel_item_selection :
0 commit comments