@@ -39,19 +39,19 @@ a simple example:
3939 ' B' : [' B0' , ' B1' , ' B2' , ' B3' ],
4040 ' C' : [' C0' , ' C1' , ' C2' , ' C3' ],
4141 ' D' : [' D0' , ' D1' , ' D2' , ' D3' ]},
42- index = [0 , 1 , 2 , 3 ])
42+ index = [0 , 1 , 2 , 3 ])
4343
4444 df2 = pd.DataFrame({' A' : [' A4' , ' A5' , ' A6' , ' A7' ],
4545 ' B' : [' B4' , ' B5' , ' B6' , ' B7' ],
4646 ' C' : [' C4' , ' C5' , ' C6' , ' C7' ],
4747 ' D' : [' D4' , ' D5' , ' D6' , ' D7' ]},
48- index = [4 , 5 , 6 , 7 ])
48+ index = [4 , 5 , 6 , 7 ])
4949
5050 df3 = pd.DataFrame({' A' : [' A8' , ' A9' , ' A10' , ' A11' ],
5151 ' B' : [' B8' , ' B9' , ' B10' , ' B11' ],
5252 ' C' : [' C8' , ' C9' , ' C10' , ' C11' ],
5353 ' D' : [' D8' , ' D9' , ' D10' , ' D11' ]},
54- index = [8 , 9 , 10 , 11 ])
54+ index = [8 , 9 , 10 , 11 ])
5555
5656 frames = [df1, df2, df3]
5757 result = pd.concat(frames)
@@ -380,7 +380,7 @@ Through the ``keys`` argument we can override the existing column names.
380380
381381.. ipython :: python
382382
383- pd.concat([s3, s4, s5], axis = 1 , keys = [' red' ,' blue' ,' yellow' ])
383+ pd.concat([s3, s4, s5], axis = 1 , keys = [' red' , ' blue' , ' yellow' ])
384384
385385 Let's consider a variation of the very first example presented:
386386
@@ -437,8 +437,8 @@ do so using the ``levels`` argument:
437437.. ipython :: python
438438
439439 result = pd.concat(pieces, keys = [' x' , ' y' , ' z' ],
440- levels = [[' z' , ' y' , ' x' , ' w' ]],
441- names = [' group_key' ])
440+ levels = [[' z' , ' y' , ' x' , ' w' ]],
441+ names = [' group_key' ])
442442
443443 .. ipython :: python
444444 :suppress:
@@ -726,9 +726,9 @@ Here is another example with duplicate join keys in DataFrames:
726726
727727.. ipython :: python
728728
729- left = pd.DataFrame({' A' : [1 ,2 ], ' B' : [2 , 2 ]})
729+ left = pd.DataFrame({' A' : [1 , 2 ], ' B' : [2 , 2 ]})
730730
731- right = pd.DataFrame({' A' : [4 ,5 , 6 ], ' B' : [2 ,2 , 2 ]})
731+ right = pd.DataFrame({' A' : [4 , 5 , 6 ], ' B' : [2 , 2 , 2 ]})
732732
733733 result = pd.merge(left, right, on = ' B' , how = ' outer' )
734734
@@ -801,8 +801,8 @@ that takes on values:
801801
802802.. ipython :: python
803803
804- df1 = pd.DataFrame({' col1' : [0 , 1 ], ' col_left' :[' a' , ' b' ]})
805- df2 = pd.DataFrame({' col1' : [1 , 2 , 2 ],' col_right' :[2 , 2 , 2 ]})
804+ df1 = pd.DataFrame({' col1' : [0 , 1 ], ' col_left' : [' a' , ' b' ]})
805+ df2 = pd.DataFrame({' col1' : [1 , 2 , 2 ], ' col_right' : [2 , 2 , 2 ]})
806806 pd.merge(df1, df2, on = ' col1' , how = ' outer' , indicator = True )
807807
808808 The ``indicator `` argument will also accept string arguments, in which case the indicator function will use the value of the passed string as the name for the indicator column.
@@ -857,19 +857,18 @@ The left frame.
857857 X = X.astype(CategoricalDtype(categories = [' foo' , ' bar' ]))
858858
859859 left = pd.DataFrame({' X' : X,
860- ' Y' : np.random.choice([' one' , ' two' , ' three' ], size = (10 ,))})
860+ ' Y' : np.random.choice([' one' , ' two' , ' three' ],
861+ size = (10 ,))})
861862 left
862863 left.dtypes
863864
864865 The right frame.
865866
866867.. ipython :: python
867868
868- right = pd.DataFrame({
869- ' X' : pd.Series([' foo' , ' bar' ],
870- dtype = CategoricalDtype([' foo' , ' bar' ])),
871- ' Z' : [1 , 2 ]
872- })
869+ right = pd.DataFrame({' X' : pd.Series([' foo' , ' bar' ],
870+ dtype = CategoricalDtype([' foo' , ' bar' ])),
871+ ' Z' : [1 , 2 ]})
873872 right
874873 right.dtypes
875874
@@ -903,11 +902,11 @@ potentially differently-indexed ``DataFrames`` into a single result
903902
904903 left = pd.DataFrame({' A' : [' A0' , ' A1' , ' A2' ],
905904 ' B' : [' B0' , ' B1' , ' B2' ]},
906- index = [' K0' , ' K1' , ' K2' ])
905+ index = [' K0' , ' K1' , ' K2' ])
907906
908907 right = pd.DataFrame({' C' : [' C0' , ' C2' , ' C3' ],
909908 ' D' : [' D0' , ' D2' , ' D3' ]},
910- index = [' K0' , ' K2' , ' K3' ])
909+ index = [' K0' , ' K2' , ' K3' ])
911910
912911 result = left.join(right)
913912
@@ -999,7 +998,7 @@ join key), using ``join`` may be more convenient. Here is a simple example:
999998
1000999 right = pd.DataFrame({' C' : [' C0' , ' C1' ],
10011000 ' D' : [' D0' , ' D1' ]},
1002- index = [' K0' , ' K1' ])
1001+ index = [' K0' , ' K1' ])
10031002
10041003 result = left.join(right, on = ' key' )
10051004
@@ -1038,8 +1037,8 @@ To join on multiple keys, the passed DataFrame must have a ``MultiIndex``:
10381037 index = pd.MultiIndex.from_tuples([(' K0' , ' K0' ), (' K1' , ' K0' ),
10391038 (' K2' , ' K0' ), (' K2' , ' K1' )])
10401039 right = pd.DataFrame({' C' : [' C0' , ' C1' , ' C2' , ' C3' ],
1041- ' D' : [' D0' , ' D1' , ' D2' , ' D3' ]},
1042- index = index)
1040+ ' D' : [' D0' , ' D1' , ' D2' , ' D3' ]},
1041+ index = index)
10431042
10441043 Now this can be joined by passing the two key column names:
10451044
@@ -1134,12 +1133,12 @@ the left argument, as in this example:
11341133
11351134 leftindex = pd.MultiIndex.from_product([list (' abc' ), list (' xy' ), [1 , 2 ]],
11361135 names = [' abc' , ' xy' , ' num' ])
1137- left = pd.DataFrame({' v1' : range (12 )}, index = leftindex)
1136+ left = pd.DataFrame({' v1' : range (12 )}, index = leftindex)
11381137 left
11391138
11401139 rightindex = pd.MultiIndex.from_product([list (' abc' ), list (' xy' )],
11411140 names = [' abc' , ' xy' ])
1142- right = pd.DataFrame({' v2' : [100 * i for i in range (1 , 7 )]}, index = rightindex)
1141+ right = pd.DataFrame({' v2' : [100 * i for i in range (1 , 7 )]}, index = rightindex)
11431142 right
11441143
11451144 left.join(right, on = [' abc' , ' xy' ], how = ' inner' )
@@ -1154,17 +1153,17 @@ done using the following code.
11541153 names = [' key' , ' X' ])
11551154 left = pd.DataFrame({' A' : [' A0' , ' A1' , ' A2' ],
11561155 ' B' : [' B0' , ' B1' , ' B2' ]},
1157- index = leftindex)
1156+ index = leftindex)
11581157
11591158 rightindex = pd.MultiIndex.from_tuples([(' K0' , ' Y0' ), (' K1' , ' Y1' ),
11601159 (' K2' , ' Y2' ), (' K2' , ' Y3' )],
11611160 names = [' key' , ' Y' ])
11621161 right = pd.DataFrame({' C' : [' C0' , ' C1' , ' C2' , ' C3' ],
11631162 ' D' : [' D0' , ' D1' , ' D2' , ' D3' ]},
1164- index = rightindex)
1163+ index = rightindex)
11651164
11661165 result = pd.merge(left.reset_index(), right.reset_index(),
1167- on = [' key' ], how = ' inner' ).set_index([' key' ,' X' ,' Y' ])
1166+ on = [' key' ], how = ' inner' ).set_index([' key' , ' X' , ' Y' ])
11681167
11691168 .. ipython :: python
11701169 :suppress:
0 commit comments