Pandas: Difference between pivot and pivot_table. Why is only pivot_table working?
Solution 1:
For anyone who is still interested in the difference between pivot
and pivot_table
, there are mainly two differences:
-
pivot_table
is a generalization ofpivot
that can handle duplicate values for one pivoted index/column pair. Specifically, you can givepivot_table
a list of aggregation functions using keyword argumentaggfunc
. The defaultaggfunc
ofpivot_table
isnumpy.mean
. -
pivot_table
also supports using multiple columns for the index and column of the pivoted table. A hierarchical index will be automatically generated for you.
REF: pivot
and pivot_table
Solution 2:
Another caveat:
pivot_table
will only allow numerical types as "values=", whereas pivot
will take string types as "values=".
Solution 3:
I debugged it a little bit.
- The
DataFrame.pivot()
andDataFrame.pivot_table()
are different. -
pivot()
doesn't accept a list for index. -
pivot_table()
accepts.
Internally, both of them are using reset_index()
/stack()
/unstack()
to do the job.
pivot()
is just a short cut for simple usage, I think.