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

Problems with Multiindex from pd.Crosstab when appending non present row data

Morning All, Happy Friday. I have some excel output which shows Client, their Sector and the counts of their Result via a crosstab command. This works well: dfAll_Clients = {'All_Client': ['AAA','BBB','CCC','DDD','EEE','FFF'], …
Peter Lucas
  • 1,979
  • 1
  • 16
  • 27
2
votes
2 answers

How to get pandas crosstab to sum up values for multiple columns?

Let's assume we have a table like: id chr val1 val2 ... A 2 10 ... B 4 20 ... A 3 30 ...and we'd like to have a contingency table like this (grouped by chr, thus using 'A' and 'B' as the…
daniel451
  • 10,626
  • 19
  • 67
  • 125
2
votes
3 answers

Making a matrix-format from python

I have the following data in my dataframe B: F1 F2 Count A C 5 B C 2 B U 6 C A 1 I want to make a square matrix out of them so the results will be: A B C U A 0 0 6 0 B 0 0 2 …
kiwi_kimchi
  • 345
  • 3
  • 12
2
votes
4 answers

Table in r to be weighted

I'm trying to run a crosstab/contingency table, but need it weighted by a weighting variable. Here is some sample data. set.seed(123) sex <- sample(c("Male", "Female"), 100, replace = TRUE) age <- sample(c("0-15", "16-29", "30-44", "45+"), 100,…
H.Cheung
  • 855
  • 5
  • 12
2
votes
4 answers

freq table for multiple variables in r

I would like to crosstab the items variable vs cat as a frequency table. df1 <- data.frame(cat = c(1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4), item1 = c(0,0,1,0,1,1,0,0,0,1,0,1,0,0,1,0,0,1), item2 =…
amisos55
  • 1,913
  • 1
  • 10
  • 21
2
votes
2 answers

Need a table with aggregated data: aggregations and crosstabs

I am doing a basic aggregation of 30 years of observations of animal individuals, and I have a rawdata file where there is one line per animal individual observation, where individual name and time of observation are parameters. I need a table with…
Dag
  • 569
  • 2
  • 5
  • 20
2
votes
1 answer

Pandas crosstab on multiindex

Hi I am trying to get Crosstab from a multi index variable "df" : df.tail() code X1 X2 X3 pays USA USA USA desc phase phase phase 2020-01-01 a a a 2020-02-01 b c d 2020-03-01 a a b 2020-04-01 c a a 2020-05-01 d …
2
votes
3 answers

How to run join on pandas crosstab

I am trying to create a Pandas crosstab, but I then want to run a join and it won't let me since I think it's a special type of DataFrame. See below for an example. df = pd.DataFrame({'A': ["Alice", "Alice", "Alice", "Bob","Bob","Bob","Charlie"],…
pn21
  • 21
  • 2
2
votes
2 answers

Create cross-tabulation in python pandas showing which values are present

given the following data: pd.DataFrame(dict( name = ['a', 'a', 'a', 'b', 'b', 'b'], vals = [1, 2 , 3, 99, 3, 4] )) which looks as: name vals 0 a 1 1 a 2 2 a 3 3 b 99 4 b 3 5 b 4 I'm wondering how…
baxx
  • 3,956
  • 6
  • 37
  • 75
2
votes
1 answer

Crosstab query with category totals, tree table structure

How would I go about making a crosstab query like this? TopItem1 TopItem2 Category1 Ca1Item1 Ca1Item2 Category2 Ca2Sub1 Ca2Sub1It1 Ca2Sub1It2 Ca2Sub2 Ca2Sub2It1 Group1 1 3 6 2 4 4 3 1 …
τεκ
  • 2,994
  • 1
  • 16
  • 14
2
votes
0 answers

Tampermonkey how to use it on two different websites

I got question like this, how to get information from one web url for example and use it in another url // ==UserScript== // @name test // @description try to take over the world! // @author You // @version 0.1 // @grant …
d4vit
  • 21
  • 3
2
votes
1 answer

How do I adjust and remove the legend labels in my crosstab plot?

Essentially, I want the dot labeled 8 to read "Disliked" and 18 to read "Liked." I want to remove the other 4 points in my legend. I'm unsure how those managed to get there. I've tried using the scale_fill_discrete() and scale_fill_manual(), but…
user12756615
2
votes
1 answer

How do I designate color levels in this crosstab visualization using ggplot?

I'm trying to create a range of colors to reflect degrees of intensity in my crosstab plot. For example, values 0-33 get assigned light green, 34-66 get assigned a standard green, and 67-100 get assigned a forest green. I have only been able to…
user12756615
2
votes
1 answer

Convert my columns in to rows in postgresql without creating any extensions

I have a data result from a view like below, But I want a view like this Can any one help me to do this via postgresql without using extensions.
2
votes
3 answers

Pivoting SQL table with crosstab function

I made this query that lists all real estate prices per m2 per year in a city. It works nice yet all years end up in rows, with the prices behind them. I would prefer seeing the years in columns with the price in a cell. Via stackoverflow I found…
Edwin Hans
  • 23
  • 3