Python Pandas.pivot() Syntax

Syntax: pandas.pivot(index, columns, values)

Parameters:

  1. index[ndarray] : Labels to use to make new frame’s index
  2. columns[ndarray] : Labels to use to make new frame’s columns
  3. values[ndarray] : Values to use for populating new frame’s values

Returns: Reshaped DataFrame
Exception: ValueError raised if there are any duplicates.

Creating a Sample DataFrame

Here, we are making a sample DataFrame that we will use in our article throughout.

Python3




# importing pandas as pd
import pandas as pd
  
# creating a dataframe
df = pd.DataFrame({'A': ['John', 'Boby', 'Mina'],
                   'B': ['Masters', 'Graduate', 'Graduate'],
                   'C': [27, 23, 21]})
  
df


Output

     A         B   C
0  John   Masters  27
1  Boby  Graduate  23
2  Mina  Graduate  21

Python | Pandas.pivot()

pandas.pivot(index, columns, values) function produces a pivot table based on 3 columns of the DataFrame. Uses unique values from the index/columns and fills them with values.

Similar Reads

Python Pandas.pivot() Syntax

Syntax: pandas.pivot(index, columns, values) Parameters: index[ndarray] : Labels to use to make new frame’s index columns[ndarray] : Labels to use to make new frame’s columns values[ndarray] : Values to use for populating new frame’s values Returns: Reshaped DataFrameException: ValueError raised if there are any duplicates....

Pandas pivot() Function Examples

...