pandas .pivot() code example

Example 1: pandas pivot

>>> df = pd.DataFrame({'foo': ['one', 'one', 'one', 'two', 'two',
...                            'two'],
...                    'bar': ['A', 'B', 'C', 'A', 'B', 'C'],
...                    'baz': [1, 2, 3, 4, 5, 6],
...                    'zoo': ['x', 'y', 'z', 'q', 'w', 't']})
>>> df

>>df.pivot(index='foo', columns='bar', values=['baz', 'zoo'])
bar  A   B   C
foo
one  1   2   3
two  4   5   6

Example 2: pivot table pandas

# Tips is the Dataframe:
# Suppose we want to compute a table of group means, we can
# use the Pivot_Table Method:

# The Pivot Table automatically computes the mean

tips.pivot_table(index=['day', 'smoker'])

# The index represents the column names that you want to form groups

Tags:

Misc Example