pivot-table

pivot_table set column as hour and index as date for a new dataframe

pivot_table set column as hour and index as date for a new dataframe Question: I need to get a new dataframe from a massive dataset so I am using pivot_table: date is like 2020-12-31 09:00:00 And I need to show something like this: import pandas as pd import datetime as dt pd.pivot_table(df_fuel, index=["date"].dt.date, columns=["date"].dt.hour, values=["Fuel_price"]) …

Total answers: 1

Pandas – compute and pivot to get revenue from previous two years

Pandas – compute and pivot to get revenue from previous two years Question: I have a dataframe like as below df = pd.DataFrame( {‘stud_id’ : [101, 101, 101, 101, 101, 102, 102, 102], ‘sub_code’ : [‘CSE01’, ‘CSE01’, ‘CSE01’, ‘CSE01’, ‘CSE02’, ‘CSE02’, ‘CSE02’, ‘CSE02’], ‘ques_date’ : [’10/11/2022′, ’06/06/2022′,’09/04/2022′, ’27/03/2022′, ’13/05/2010′, ’10/11/2021′,’11/1/2022′, ’27/02/2022′], ‘revenue’ : [77, 86, …

Total answers: 1

How to subtract a in second level columns in multiIndex level dataframe

How to subtract second level columns in multiIndex level dataframe Question: Here is the example data I am working with. What I am trying to accomplish is 1) subtract b column from column a and 2) create the C column in front of a and b columns. I would like to loop through and create …

Total answers: 3

Pd.crosstab missing data?

Pd.crosstab missing data? Question: I am using pd.crosstab to count presence/absence data. In the first column, I have several presence counts (represented by 1’s), in the second column I have just one ‘presence’. Howwever, when I run crosstab on this data that single presence in the second column isn’t counted. Could anyone shed some light …

Total answers: 1

How to create a multi-index pivot table that sums the max values within a sub-group

How to create a multi-index pivot table that sums the max values within a sub-group Question: I have a somewhat large dataframe of customers assigned to a hub and each hub is in a specific location. The hubs get flagged whenever there’s an issue and I’d like to know the number of customers affected each …

Total answers: 1

TypeError: unhashable type: 'list' when I try to do a pivot from a column in pandas

TypeError: unhashable type: 'list' when I try to do a pivot from a column in pandas Question: My Python code takes a bank statement from Excel and creates a dataframe that categorises each transaction based on description. Example code: import pandas as pd import openpyxl import datetime as dt import numpy as np dff = …

Total answers: 2

Different questions about pandas pivot tables

Different questions about pandas pivot tables Question: Here’s my df: df=pd.DataFrame( { ‘Color’: [‘red’,’blue’,’red’,’red’,’green’,’red’,’yellow’], ‘Type’: [‘Oil’, ‘Aluminium’, ‘Oil’, ‘Oil’, ‘Cement Paint’, ‘Synthetic Rubber’, ‘Emulsion’], ‘Finish’ : [‘Satin’, ‘Matte’, ‘Matte’, ‘Satin’, ‘Semi-gloss’, ‘Satin’, ‘Satin’], ‘Use’ : [‘Interior’, ‘Exterior’, ‘Interior’, ‘Interior’, ‘Exterior’, ‘Exterior’, ‘Exterior’], ‘Price’ : [55, 75, 60, 60, 55, 75, 50] } ) I want …

Total answers: 1

How to sort Pandas crosstab columns by sum of values

How to sort Pandas crosstab columns by sum of values Question: I have a crosstab table with 4 rows and multiple columns, containing numeral values (representing the number of dataset elements on the crossing of two factors). I want to sort the order of columns in the crosstab by the sum of values in each …

Total answers: 1

Transforming Pandas pivot-table to multiple arrays of objects

Transforming Pandas pivot-table to multiple arrays of objects Question: I have a Pandas pivot table; The goal is to pass this to a Django rest framework in the form of multiple arrays which I can easily filter in React JavaScript. pivot: x y z Magazine date M1 2018-01 173 68 10 2018-02 184 55 11 …

Total answers: 2