pivot

Pandas pivot table: columns order and subtotals

こ雲淡風輕ζ 提交于 2021-02-07 03:28:07
问题 I'm using Pandas 0.19. Considering the following data frame: FID admin0 admin1 admin2 windspeed population 0 cntry1 state1 city1 60km/h 700 1 cntry1 state1 city1 90km/h 210 2 cntry1 state1 city2 60km/h 100 3 cntry1 state2 city3 60km/h 70 4 cntry1 state2 city4 60km/h 180 5 cntry1 state2 city4 90km/h 370 6 cntry2 state3 city5 60km/h 890 7 cntry2 state3 city6 60km/h 120 8 cntry2 state3 city6 90km/h 420 9 cntry2 state3 city6 120km/h 360 10 cntry2 state4 city7 60km/h 740 How can I create a table

WHERE clause great than zero still showing zero when using CASE functions

泪湿孤枕 提交于 2021-02-05 11:19:26
问题 I am trying the WHERE clause to filter out other data I dont want. SELECT `post_id`, MAX(CASE WHEN `meta_key` = 'vlaue_1' THEN `meta_value` ELSE NULL END) as 'Customer', MAX(CASE WHEN `meta_key` = 'value_2' THEN `meta_value` ELSE NULL END) as 'DeliveryDate', MAX(CASE WHEN `meta_key` = 'value_3' THEN `meta_value` ELSE NULL END) as 'DeliveryTime', MAX(CASE WHEN `meta_key` = 'vlaue_4' THEN `meta_value` ELSE NULL END) as 'DeliveryType' FROM wp_postmeta WHERE 'Customer' > 0 GROUP BY `post_id`

Transform one to many data to columns

狂风中的少年 提交于 2021-02-05 08:10:52
问题 Given this data: How do I transform it to look like this: There are two columns in the data source, key ( title ) and value ( responsibility ). I need to transform it such that we have the key column ( title ) and then n columns where n is the highest number of value a key has, eg 3 in the picture above. Hence the columns should be: Title, 1, 2, 3 . The values in each column 1, 2, 3 should be corresponding to values in the original data. Any combination of formula is welcomed - I believe a

Reshape horizontal to to long format using pivot_longer

久未见 提交于 2021-02-05 08:09:10
问题 I am trying to reshape my data to long instead of wide format using the same code provided earlier link , however it doesn't work even after several trials to modify names_pattern = "(.*)_(pre|post.*)", My data sample is data1<-read.table(text=" Serial_ID pre_EDV pre_ESV pre_LVEF post_EDV post_ESV post_LVEF 1 76.2 32.9 56.8 86.3 36.6 57.6 2 65.4 35.9 45.1 60.1 26.1 56.7 3 64.4 35.1 45.5 72.5 41.1 43.3 4 50 13.9 72.1 46.4 18.4 60.4 5 89.6 32 64.3 70.9 19.3 72.8 6 62 20.6 66.7 55.9 17.8 68.2 7

Storing dynamic Pivot result into a temporary table in SQL Server

ε祈祈猫儿з 提交于 2021-02-05 07:18:45
问题 I have the following schema and sample data. create table MyTable ( Id int, Year int, Channel varchar(10), Payments int ) insert into MyTable values (1,2012,'HV',100), (1,2014,'HV',56), (2,2012,'NL',17000) (2,2012,'HV',495), (3,2013,'HV',565) Now I want to create and insert dynamic pivot data in a temporary table. I am able to create the pivot data as the demo here. But I want to store this data into a temporary table. What I have tried is as below. Declare @SQL varchar(max) = ' if object_id(

How do I group max and min timestamp on pandas dataframe

浪子不回头ぞ 提交于 2021-02-05 06:42:04
问题 I want to group a dataset and return the maximum and minimum timestamp. Here's my data id timestamp 1 2017-09-17 10:09:01 2 2017-10-02 01:13:15 1 2017-09-17 10:53:07 1 2017-09-17 10:52:18 2 2017-09-12 21:59:40 Here's the output that i want id max min 1 2017-09-17 10:53:07 2017-09-17 10:09:01 2 2017-10-02 01:13:15 2017-09-12 21:59:40 Here's what I did, the code seems not efficient, I hope theres better way to do this on pandas data1 = df.sort_values('timestamp').drop_duplicates(['customer_id']

SQL Server dynamic pivot with multiple columns

泄露秘密 提交于 2021-02-04 21:45:11
问题 Here's the scenario I am in. I have my data in the following format. My source data IssuedOn Country Sales Transactions ------------------------------------------ 29-Aug-16 India 40 8 29-Aug-16 Australia 15 3 29-Aug-16 Canada 15 3 30-Aug-16 India 50 10 30-Aug-16 Australia 25 5 30-Aug-16 Canada 10 2 31-Aug-16 India 100 25 31-Aug-16 Australia 30 10 31-Aug-16 Canada 55 12 This is the output I am looking for Expected output IssuedDate Australia Canada India TotalSales Transactionscount ----------

SQL Server dynamic pivot with multiple columns

混江龙づ霸主 提交于 2021-02-04 21:44:24
问题 Here's the scenario I am in. I have my data in the following format. My source data IssuedOn Country Sales Transactions ------------------------------------------ 29-Aug-16 India 40 8 29-Aug-16 Australia 15 3 29-Aug-16 Canada 15 3 30-Aug-16 India 50 10 30-Aug-16 Australia 25 5 30-Aug-16 Canada 10 2 31-Aug-16 India 100 25 31-Aug-16 Australia 30 10 31-Aug-16 Canada 55 12 This is the output I am looking for Expected output IssuedDate Australia Canada India TotalSales Transactionscount ----------

athena presto - multiple columns from long to wide

柔情痞子 提交于 2021-02-04 08:37:25
问题 I am new to Athena and I am trying to understand how to turn multiple columns from long to wide format. It seems like presto is what is needed, but I've only successfully been able to apply map_agg to one variable. I think my below final outcome can be achieved with multimap_agg but cannot quite get it to work. Below I walk through my steps and data. If you have some suggestions or questions, please let me know! First, the data starts like this: id | letter | number | value ------------------

pandas: unstack rows into new columns

被刻印的时光 ゝ 提交于 2021-02-04 06:52:06
问题 i have a df that looks like this: a date c 0 ABC 2020-06-01 0.1 1 ABC 2020-05-01 0.2 2 DEF 2020-07-01 0.3 3 DEF 2020-01-01 0.4 4 DEF 2020-02-01 0.5 5 DEF 2020-07-01 0.6 i would like to "unstack" column 'a' so my new df looks like this a date1 c1 date2 c2 date3 c3 date4 c4 0 ABC 2020-06-01 0.1 2020-05-01 0.2 nan nan nan nan 1 DEF 2020-07-01 0.3 2020-01-01 0.4 2020-02-01 0.5 2020-07-01 0.6 how can i do this? 回答1: Use GroupBy.cumcount for helper counter for MultiIndex and reshape by DataFrame