@@ -18,12 +18,12 @@ Reshaping by pivoting DataFrame objects
1818 tm.N = 3
1919
2020 def unpivot (frame ):
21- N, K = frame.shape
22- data = {' value' : frame.to_numpy().ravel(' F' ),
23- ' variable' : np.asarray(frame.columns).repeat(N),
24- ' date' : np.tile(np.asarray(frame.index), K)}
25- columns = [' date' , ' variable' , ' value' ]
26- return pd.DataFrame(data, columns = columns)
21+ N, K = frame.shape
22+ data = {' value' : frame.to_numpy().ravel(' F' ),
23+ ' variable' : np.asarray(frame.columns).repeat(N),
24+ ' date' : np.tile(np.asarray(frame.index), K)}
25+ columns = [' date' , ' variable' , ' value' ]
26+ return pd.DataFrame(data, columns = columns)
2727
2828 df = unpivot(tm.makeTimeDataFrame())
2929
@@ -371,8 +371,8 @@ Consider a data set like this:
371371 ' C' : [' foo' , ' foo' , ' foo' , ' bar' , ' bar' , ' bar' ] * 4 ,
372372 ' D' : np.random.randn(24 ),
373373 ' E' : np.random.randn(24 ),
374- ' F' : [datetime.datetime(2013 , i, 1 ) for i in range (1 , 13 )] +
375- [datetime.datetime(2013 , i, 15 ) for i in range (1 , 13 )]})
374+ ' F' : [datetime.datetime(2013 , i, 1 ) for i in range (1 , 13 )]
375+ + [datetime.datetime(2013 , i, 15 ) for i in range (1 , 13 )]})
376376 df
377377
378378 We can produce pivot tables from this data very easily:
@@ -397,7 +397,8 @@ Also, you can use ``Grouper`` for ``index`` and ``columns`` keywords. For detail
397397
398398.. ipython :: python
399399
400- pd.pivot_table(df, values = ' D' , index = pd.Grouper(freq = ' M' , key = ' F' ), columns = ' C' )
400+ pd.pivot_table(df, values = ' D' , index = pd.Grouper(freq = ' M' , key = ' F' ),
401+ columns = ' C' )
401402
402403 You can render a nice output of the table omitting the missing values by
403404calling ``to_string `` if you wish:
@@ -693,6 +694,7 @@ handling of NaN:
693694
694695.. code-block :: ipython
695696
697+ In [1]: x = pd.Series(['A', 'A', np.nan, 'B', 3.14, np.inf])
696698 In [2]: pd.factorize(x, sort=True)
697699 Out[2]:
698700 (array([ 2, 2, -1, 3, 0, 1]),
@@ -721,7 +723,8 @@ DataFrame will be pivoted in the answers below.
721723 n = 20
722724
723725 cols = np.array([' key' , ' row' , ' item' , ' col' ])
724- df = cols + pd.DataFrame((np.random.randint(5 , size = (n, 4 )) // [2 , 1 , 2 , 1 ]).astype(str ))
726+ df = cols + pd.DataFrame((np.random.randint(5 , size = (n, 4 ))
727+ // [2 , 1 , 2 , 1 ]).astype(str ))
725728 df.columns = cols
726729 df = df.join(pd.DataFrame(np.random.rand(n, 2 ).round(2 )).add_prefix(' val' ))
727730
@@ -734,7 +737,7 @@ Suppose we wanted to pivot ``df`` such that the ``col`` values are columns,
734737``row `` values are the index, and the mean of ``val0 `` are the values? In
735738particular, the resulting DataFrame should look like:
736739
737- .. code-block :: ipython
740+ .. note ::
738741
739742 col col0 col1 col2 col3 col4
740743 row
0 commit comments