pandas pivot_table SQL equivalent

Viewed 101

Can we in SQL (preferably Postgresql) mimic the pandas function pivot_table?

For example, let's say we have a table with the following 3 columns:

   Name        Day       Value
   John     Sunday           6
   John     Monday           3
   John     Tuesday          2
   Mary     Sunday           6
   Mary     Monday           4
   Mary     Tuesday          7
   Alex     Tuesday          1

I want to pivot the table so that the index is the name, the columns are the days, and cells are the values:

    names  Monday  Sunday  Tuesday
    John       3       6        2
    Mary       4       6        7
    Alex     null    null       1

Part of the example was taken from the question Transform a 3-column dataframe into a matrix

0 Answers
Related