DataFrame.pivot
vs pandas.pivot_table
¶
- Both
DataFrame.pivot
andpandas.pivot_table
can generate pivot tables.pandas.pivot_table
aggregate values whileDataFrame.pivot
not.
In [3]:
import pandas as pd
import numpy as np
df = pd.DataFrame(
{"id": ["a", "b", "c"], "x": [1, 3, 5], "y": [2, 4, 6], "z": [7, 8, 9]}
)
df
Out[3]:
In [5]:
dfm = pd.melt(df, id_vars="id", value_vars=["x", "y", "z"])
dfm
Out[5]:
DataFrame.pivot
¶
The values option is not flexible at this time. It accepts a column name or None but cannot accept a list of columns at this time. One way to achieve it is to specify None for values and then select columns you want manually.
In [6]:
dfp = dfm.pivot(index="id", columns="variable", values="value")
dfp
Out[6]:
In [36]:
dfp.index
Out[36]:
In [ ]:
unstack pivot_table, ...
In [ ]:
jj = j.unstack()
pandas.pivot_table
¶
In [7]:
pd.pivot_table(dfm, index="id", columns="variable", values="value")
Out[7]:
In [8]:
df = pd.DataFrame(
data=[
["foo", "one", "small", 1],
["foo", "one", "large", 2],
["foo", "one", "large", 2],
["foo", "two", "small", 3],
["foo", "two", "small", 3],
["bar", "one", "large", 4],
["bar", "one", "small", 5],
["bar", "two", "small", 6],
["bar", "two", "large", 7],
],
columns=["a", "b", "c", "d"],
)
df
Out[8]:
In [51]:
pd.pivot_table(df, index=["a", "b"], columns="c", values="d", aggfunc=np.sum)
Out[51]:
In [ ]: