In [1]:
import numpy as np
import pandas as pd
In [2]:
df = pd.DataFrame({'s1': ['one', 'one', 'one', 'two', 'two',
                            'two'],
                    's2': ['P', 'Q', 'R', 'P', 'Q', 'R'],
                    's3': [2, 3, 4, 5, 6, 7],
                    's4': ['x', 'y', 'z', 'q', 'w', 't']})
df
Out[2]:
s1 s2 s3 s4
0 one P 2 x
1 one Q 3 y
2 one R 4 z
3 two P 5 q
4 two Q 6 w
5 two R 7 t
In [3]:
df.pivot(index='s1', columns='s2', values='s3')
Out[3]:
s2 P Q R
s1
one 2 3 4
two 5 6 7
In [4]:
df.pivot(index='s1', columns='s2')['s3']
Out[4]:
s2 P Q R
s1
one 2 3 4
two 5 6 7
In [5]:
df.pivot(index='s1', columns='s2', values=['s3', 's4'])
Out[5]:
s3 s4
s2 P Q R P Q R
s1
one 2 3 4 x y z
two 5 6 7 q w t

A ValueError is raised if there are any duplicates.

In [6]:
df = pd.DataFrame({"s1": ['one', 'one', 'two', 'two'],
                   "s2": ['P', 'P', 'Q', 'R'],
                   "s3": [1, 2, 4, 5]})
df
Out[6]:
s1 s2 s3
0 one P 1
1 one P 2
2 two Q 4
3 two R 5

Notice that the first two rows are the same for our index and columns arguments.

In [7]:
df.pivot(index='s1', columns='s2', values='s3')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-1df4a16e9afd> in <module>
----> 1 df.pivot(index='s1', columns='s2', values='s3')

~\Anaconda3\lib\site-packages\pandas\core\frame.py in pivot(self, index, columns, values)
   5917         from pandas.core.reshape.pivot import pivot
   5918 
-> 5919         return pivot(self, index=index, columns=columns, values=values)
   5920 
   5921     _shared_docs[

~\Anaconda3\lib\site-packages\pandas\core\reshape\pivot.py in pivot(data, index, columns, values)
    428         else:
    429             indexed = data._constructor_sliced(data[values].values, index=index)
--> 430     return indexed.unstack(columns)
    431 
    432 

~\Anaconda3\lib\site-packages\pandas\core\series.py in unstack(self, level, fill_value)
   3743         from pandas.core.reshape.reshape import unstack
   3744 
-> 3745         return unstack(self, level, fill_value)
   3746 
   3747     # ----------------------------------------------------------------------

~\Anaconda3\lib\site-packages\pandas\core\reshape\reshape.py in unstack(obj, level, fill_value)
    421             level=level,
    422             fill_value=fill_value,
--> 423             constructor=obj._constructor_expanddim,
    424         )
    425         return unstacker.get_result()

~\Anaconda3\lib\site-packages\pandas\core\reshape\reshape.py in __init__(self, values, index, level, value_columns, fill_value, constructor)
    140 
    141         self._make_sorted_values_labels()
--> 142         self._make_selectors()
    143 
    144     def _make_sorted_values_labels(self):

~\Anaconda3\lib\site-packages\pandas\core\reshape\reshape.py in _make_selectors(self)
    178 
    179         if mask.sum() < len(self.index):
--> 180             raise ValueError("Index contains duplicate entries, " "cannot reshape")
    181 
    182         self.group_index = comp_index

ValueError: Index contains duplicate entries, cannot reshape