Questions tagged [pivot-table]

A "pivot table" is a method of summarizing data in tabular form. Typically, these have a category column in the left hand side, and multiple summary columns to the right. Each different category will have its own row. Be sure to include the relevant language tag (python, excel, R, postgreSQL etc.) on your question.

A "pivot table" is a method of summarizing data in tabular form. Typically, these have a category column in the left hand side, and multiple summary columns to the right. Each different category will have its own row.

Pivot tables are typically found in data visualization programs, including spreadsheets and business intelligence. Note that a "pivot table" is also known as a "cross tab" or "cross tabulation".

Resources:

8105 questions
383
votes
11 answers

How can I return pivot table output in MySQL?

If I have a MySQL table looking something like this: company_name action pagecount ------------------------------- Company A PRINT 3 Company A PRINT 2 Company A PRINT 3 Company B EMAIL Company B PRINT …
peku
  • 5,003
  • 3
  • 20
  • 15
328
votes
9 answers

Convert Rows to columns using 'Pivot' in SQL Server

I have read the stuff on MS pivot tables and I am still having problems getting this correct. I have a temp table that is being created, we will say that column 1 is a Store number, and column 2 is a week number and lastly column 3 is a total of…
Lynn
  • 3,534
  • 5
  • 18
  • 18
265
votes
13 answers

MySQL - Rows to Columns

I tried to search posts, but I only found solutions for SQL Server/Access. I need a solution in MySQL (5.X). I have a table (called history) with 3 columns: hostid, itemname, itemvalue. If I do a select (select * from history), it will return …
Bob Rivers
  • 5,261
  • 6
  • 47
  • 59
186
votes
7 answers

Is it possible to Pivot data using LINQ?

I am wondering if it is possible to use LINQ to pivot data from the following layout: CustID | OrderDate | Qty 1 | 1/1/2008 | 100 2 | 1/2/2008 | 200 1 | 2/2/2008 | 350 2 | 2/28/2008 | 221 1 | 3/12/2008 | 250 2 |…
Tim Lentine
  • 7,782
  • 5
  • 35
  • 40
139
votes
16 answers

Simple Pivot Table to Count Unique Values

This seems like a simple Pivot Table to learn with. I would like to do a count of unique values for a particular value I'm grouping on. For instance, I have this: ABC 123 ABC 123 ABC 123 DEF 456 DEF 567 DEF 456 DEF 456 What I want is…
user1586422
  • 1,393
  • 2
  • 9
  • 4
105
votes
4 answers

Difference between groupby and pivot_table for pandas dataframes

I just started learning Pandas and was wondering if there is any difference between groupby and pivot_table functions. Can anyone help me understand the difference between them?
user4943236
  • 5,914
  • 11
  • 27
  • 40
88
votes
9 answers

How to aggregate unique count with pandas pivot_table

This code: df2 = ( pd.DataFrame({ 'X' : ['X1', 'X1', 'X1', 'X1'], 'Y' : ['Y2', 'Y1', 'Y1', 'Y1'], 'Z' : ['Z3', 'Z1', 'Z1', 'Z2'] }) ) g = df2.groupby('X') pd.pivot_table(g, values='X', rows='Y', cols='Z',…
dmi
  • 1,865
  • 3
  • 19
  • 19
83
votes
3 answers

getting the value of an extra pivot table column laravel

I have a phone_models, phone_problems, and a phone_model_phone_problem pivot table. The pivot table has an extra column 'price'. PhoneModel: class PhoneModel extends \Eloquent { public function problems() { return…
Rodrigo
  • 3,129
  • 3
  • 33
  • 61
60
votes
9 answers

Difference between pivot and pivot_table. Why is only pivot_table working?

I have the following dataframe. df.head(30) struct_id resNum score_type_name score_value 0 4294967297 1 omega 0.064840 1 4294967297 1 fa_dun 2.185618 2 4294967297 1 fa_dun_dev …
jwillis0720
  • 4,329
  • 8
  • 41
  • 74
54
votes
2 answers

Pandas groupby with bin counts

I have a DataFrame that looks like this: +----------+---------+-------+ | username | post_id | views | +----------+---------+-------+ | john | 1 | 3 | | john | 2 | 23 | | john | 3 | 44 | | john | 4 |…
metersk
  • 11,803
  • 21
  • 63
  • 100
54
votes
10 answers

How is a pivot table created by Laravel?

In Laravel 4, when working with many-to-many relationships as described in the 4.2 docs, how can I actually get Laravel to create the pivot table for me? Do I need to add something in my migrations for the two models that are involved? Do I need to…
Ben
  • 15,938
  • 19
  • 92
  • 138
49
votes
6 answers

Pandas Pivot tables row subtotals

I'm using Pandas 0.10.1 Considering this Dataframe: Date State City SalesToday SalesMTD SalesYTD 20130320 stA ctA 20 400 1000 20130320 stA ctB 30 500 1100 20130320 stB ctC…
Ivan Pereira
  • 2,149
  • 3
  • 24
  • 32
46
votes
6 answers

How to get rid of multilevel index after using pivot table pandas?

I had following data frame (the real data frame is much more larger than this one ) : sale_user_id sale_product_id count 1 1 1 1 8 1 1 52 1 1 …
chessosapiens
  • 3,159
  • 10
  • 36
  • 58
44
votes
2 answers

Pivot Tables of Counts in Pandas DataFrame

I have a pandas dataframe: Col X Col Y class 1 cat 1 class 2 cat 1 class 3 cat 2 class 2 cat 3 that I want to transform into: cat 1 cat 2 cat 3 class 1 1 0 0 class 2 1 0 1 class 3 0 …
SteelyDanish
  • 629
  • 2
  • 8
  • 15
41
votes
5 answers

Laravel attach pivot to table with multiple values

Background I'm creating a database revolving around food allergies and I have a many to many relationship between foods and allergies. There is also a pivot value called severity which has a numerical number representing the severity of the allergy…
Duncan Ogle
  • 784
  • 2
  • 7
  • 17
1
2 3
99 100