@@ -18,12 +18,12 @@ Reshaping by pivoting DataFrame objects
18
18
tm.N = 3
19
19
20
20
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)
27
27
28
28
df = unpivot(tm.makeTimeDataFrame())
29
29
@@ -371,8 +371,8 @@ Consider a data set like this:
371
371
' C' : [' foo' , ' foo' , ' foo' , ' bar' , ' bar' , ' bar' ] * 4 ,
372
372
' D' : np.random.randn(24 ),
373
373
' 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 )]})
376
376
df
377
377
378
378
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
397
397
398
398
.. ipython :: python
399
399
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' )
401
402
402
403
You can render a nice output of the table omitting the missing values by
403
404
calling ``to_string `` if you wish:
@@ -693,6 +694,7 @@ handling of NaN:
693
694
694
695
.. code-block :: ipython
695
696
697
+ In [1]: x = pd.Series(['A', 'A', np.nan, 'B', 3.14, np.inf])
696
698
In [2]: pd.factorize(x, sort=True)
697
699
Out[2]:
698
700
(array([ 2, 2, -1, 3, 0, 1]),
@@ -721,7 +723,8 @@ DataFrame will be pivoted in the answers below.
721
723
n = 20
722
724
723
725
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 ))
725
728
df.columns = cols
726
729
df = df.join(pd.DataFrame(np.random.rand(n, 2 ).round(2 )).add_prefix(' val' ))
727
730
@@ -734,7 +737,7 @@ Suppose we wanted to pivot ``df`` such that the ``col`` values are columns,
734
737
``row `` values are the index, and the mean of ``val0 `` are the values? In
735
738
particular, the resulting DataFrame should look like:
736
739
737
- .. code-block :: ipython
740
+ .. note ::
738
741
739
742
col col0 col1 col2 col3 col4
740
743
row
0 commit comments