Questions tagged [crosstab]

A cross tab, also known as 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 also include the relevant language tag (python, R, sql-server etc.) on your question.

Resources:

1549 questions
2
votes
1 answer

Create count table under specific condition

I have a set of binary variables (with the values of 0 and 1) and I want to create a two-way count table that summarizes the counts of cooccurrence of pairs of variables (i.e., both of them have the value of 1). Here is an example dataset: mm <-…
cliu
  • 933
  • 6
  • 13
2
votes
2 answers

Pivot table on postgresql

I'm trying to make a table pivot on a postgresql database but i can't succeed. Here is my original request SELECT tag_country.label, COUNT(*) FROM log_event_tracking LEFT JOIN user_account ON log_event_tracking.email = user_account.email LEFT JOIN…
2
votes
1 answer

How to display unassigned rows while using Crosstab in Sqlserver

I have used crosstab in my query to convert row to column according to requirement. Still I am a away from the desired result. Here I have given some test data Declare @tblDepartment Table ( DepartmentID int, DepartmentName nvarchar(10)…
Rumina
  • 23
  • 2
2
votes
1 answer

Use R's Table function to cross-tabulate data grouped by another variable

Background Here's a dataframe d: d <- data.frame(ID = c("a","a","b","b"), product_code = c("B78","X31","C12","C12"), multiple_products = c(1,1,0,0), stringsAsFactors=FALSE) The…
logjammin
  • 1,121
  • 6
  • 21
2
votes
0 answers

Display panda crosstab as a table

Is there any way to show panda cross tab as a clean table? I use tabulate and it's not like what I want. input : print(tabulate(pd.crosstab(y_test,y_pred))) output: - -- -- 2 71 23 3 20 74 - -- -- I need some clean output like what Jupyter…
narsan
  • 62
  • 10
2
votes
1 answer

Posgresql crosstab alternative

I am looking for a crosstab alternative. I have data in Timescaledb (posgresql) hypertable in multiple rows: For example I need to calculate average of category = 1 when category 2 > 4. What I am doing at them moment is to pivot using crosstab and…
Kylo
  • 109
  • 8
2
votes
1 answer

How can I flatten the output dataframe of pandas crosstab from two series x and y into a series?

I have the following series x and y: x = pd.Series(['a', 'b', 'a', 'c', 'c'], name='x') y = pd.Series([1, 0, 1, 0, 0], name='y') I call pd.crosstab to get the following dataframe as output: pd.crosstab(x, y) Output: y 0 1 x a 0 2 b …
Anand
  • 3,690
  • 4
  • 33
  • 64
2
votes
1 answer

change multi level index fonts with pandas style function

I want to make some nice tables with a multi-level index (cross-tabulated across multiple variables). With the data below, the indexes for place, band and status are rather large and in charge if i print it with pandas style function. Is there a way…
b101
  • 287
  • 1
  • 8
2
votes
1 answer
2
votes
1 answer

Python Pandas Cross Tab By Multiple Categories and Years

I have a dataframe with the following simplified structure: import pandas as pd d = {'id': [1, 2, 3, 4, 5], 'name': ["a", "b", "c", "d", "e"], 'country': ["uk", "spain", "france", "germany", "italy"], 'cat_01_2020': [10, 20, 30, 40,…
2
votes
1 answer

Postgresql Crosstab with Unknown Result

I'm trying create a Crosstab query to retrieve custom values from my database, and match them to a parent entity. I have the crosstab functioning, but the problem I have is that my results are entirely unknown. To explain, my application features a…
Curtis Snowden
  • 407
  • 1
  • 4
  • 20
2
votes
1 answer

Making Pandas dataframe to display aggregate values based on date

Hello a Python newbie here. I have a dataframe that shows the product and how much they sold on each date I need to change this dataframe to show the aggregate amount of units sold. This is just an example dataframe and the actual dataframe that I…
Adam Lee
  • 99
  • 1
  • 4
  • 13
2
votes
2 answers

Reindex columns after using crosstab with regular month order instead of alphabetic

I have the following output after use crosstab, see image attached: My question is the following: How can I sort the columns by month based on year position instead of by alphabetical order?
2
votes
1 answer

Pass / Fail Dataframe example

I have the followign code: import pandas as pd status = ['Pass','Fail'] item_info = pd.DataFrame({ 'student': ['John','Alice','Pete','Mike','John','Alice','Joseph'], 'test':…
2
votes
1 answer

How to crosstab 4 variables using tabyl in R

I want to crosstab variables x , y, z by group g. I can do it with 3 variables (tabyl(x,y,z)) but I want to repeat it for each value of g. I tried group_by(g) but it didn't work. library(dplyr) #> #> Attaching package: 'dplyr' #> The following…
sbac
  • 1,897
  • 1
  • 18
  • 31