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
41
votes
5 answers

PostgreSQL convert columns to rows? Transpose?

I have a PostgreSQL function (or table) which gives me the following output: Sl.no username Designation salary etc.. 1 A XYZ 10000 ... 2 B RTS 50000 ... 3 C …
DonRaHulk
  • 575
  • 1
  • 6
  • 18
39
votes
5 answers

Convert matrix to 3-column table ('reverse pivot', 'unpivot', 'flatten', 'normalize')

I need to convert the Excel matrix FIRST in the table LATER: FIRST: P1 P2 P3 P4 F1 X F2 X X F3 X X F4 X X LATER: F P VALUE F1 P1 X F1 P2 F1 P3 F1 P4 F2 P1 X F2 P2 …
user3095042
  • 401
  • 1
  • 5
  • 4
39
votes
3 answers

How to filter a pivot table using Eloquent?

I'm using a pivot table on the project I'm working with to get works of users. E.g: User::find(1)->works gives me the works of user with ID of 1. The thing is that I want to filter this results with extra Pivot data. Something…
Arda
  • 6,756
  • 3
  • 47
  • 67
38
votes
10 answers

How do you create a "reverse pivot" in Google Sheets?

I am trying to produce a "reverse pivot" function. I have searched long and hard for such a function, but cannot find one that is already out there. I have a summary table with anywhere up to 20 columns and hundreds of rows, however I would like to…
37
votes
5 answers

How is a Pandas crosstab different from a Pandas pivot_table?

Both the pandas.crosstab and the Pandas pivot table seem to provide the exact same functionality. Are there any differences?
user1008537
36
votes
2 answers

How can I "unpivot" specific columns from a pandas DataFrame?

I have a pandas DataFrame, eg: df = pd.DataFrame({'farm' : ['A','B','A','B'], 'fruit':['apple','apple','pear','pear'], '2014':[10,12,6,8], '2015':[11,13,7,9]}) ie: 2014 2015 farm …
Racing Tadpole
  • 4,270
  • 6
  • 37
  • 56
35
votes
3 answers

pandas pivot_table column names

For a dataframe like this: d = {'id': [1,1,1,2,2], 'Month':[1,2,3,1,3],'Value':[12,23,15,45,34], 'Cost':[124,214,1234,1324,234]} df = pd.DataFrame(d) Cost Month Value id 0 124 1 12 1 1 214 2 23 1 2 …
muon
  • 12,821
  • 11
  • 69
  • 88
33
votes
2 answers

Laravel adding data to pivot table while inserting new record

I have three tables, roles(id, name); users(id, email, password); user_role(id, user_id, role_id); In this scenario, I have users table is associated to roles table with many to many relation. I have two eloquent model as Role +users(){ …
Dipendra Gurung
  • 5,720
  • 11
  • 39
  • 62
31
votes
5 answers

Laravel: find if a pivot table record exists

I have two models which are joined by a pivot table, User and Task. I have a user_id and a task_id. What is the neatest way to check whether a record exists for this combination of user and task?
datavoredan
  • 3,536
  • 9
  • 32
  • 48
30
votes
3 answers

Pivoting a Pandas Dataframe containing strings - 'No numeric types to aggregate' error

There is a good number of questions about this error, but after looking around I'm still not able to find/wrap my mind around a solution yet. I'm trying to pivot a data frame with strings, to get some row data to become columns, but not working out…
jmhead
  • 887
  • 1
  • 12
  • 25
30
votes
8 answers

Where NOT in pivot table

In Laravel we can setup relationships like so: class User { public function items() { return $this->belongsToMany('Item'); } } Allowing us to to get all items in a pivot table for a user: Auth::user()->items(); However what if…
Rob
  • 10,851
  • 21
  • 69
  • 109
29
votes
1 answer

mysql select dynamic row values as column names, another column as value

I have a legacy table of user information (that is still in active use) and I cannot change the structure of - id name value ------------------------------ 0 timezone Europe/London 0 language en 0 country 45 0 …
gingerCodeNinja
  • 1,239
  • 1
  • 12
  • 27
27
votes
5 answers

Pivot data using LINQ

I have a collection of items that contain an Enum (TypeCode) and a User object, and I need to flatten it out to show in a grid. It's hard to explain, so let me show a quick example. Collection has items like so: TypeCode | User --------------- 1 …
Dallas
26
votes
3 answers

how to pivot/unpivot (cast/melt) data frame?

How can I 'unpivot' a table? What is the proper technical term for this? UPDATE: The term is called melt I have a data frame for countries and data for each year Country 2001 2002 2003 Nigeria 1 2 3 UK 2 NA …
pedrosaurio
  • 4,708
  • 11
  • 39
  • 53
25
votes
2 answers

Simple cross-tabulation in pandas

I stumbled across pandas and it looks ideal for simple calculations that I'd like to do. I have a SAS background and was thinking it'd replace proc freq -- it looks like it'll scale to what I may want to do in the future. However, I just can't seem…
Jon Clements
  • 138,671
  • 33
  • 247
  • 280