melt

Melting pandas data frame with multiple variable names and multiple value names

余生长醉 提交于 2020-11-27 05:03:57
问题 How can I melt a pandas data frame using multiple variable names and values? I have the following data frame that changes its shape in a for loop. In one of the for loop iterations, it looks like this: ID Cat Class_A Class_B Prob_A Prob_B 1 Veg 1 2 0.9 0.1 2 Veg 1 2 0.8 0.2 3 Meat 1 2 0.6 0.4 4 Meat 1 2 0.3 0.7 5 Veg 1 2 0.2 0.8 I need to melt it in such a way that it looks like this: ID Cat Class Prob 1 Veg 1 0.9 1 Veg 2 0.1 2 Veg 1 0.8 2 Veg 2 0.2 3 Meat 1 0.6 3 Meat 2 0.4 4 Meat 1 0.3 4

Melting pandas data frame with multiple variable names and multiple value names

為{幸葍}努か 提交于 2020-11-27 05:03:13
问题 How can I melt a pandas data frame using multiple variable names and values? I have the following data frame that changes its shape in a for loop. In one of the for loop iterations, it looks like this: ID Cat Class_A Class_B Prob_A Prob_B 1 Veg 1 2 0.9 0.1 2 Veg 1 2 0.8 0.2 3 Meat 1 2 0.6 0.4 4 Meat 1 2 0.3 0.7 5 Veg 1 2 0.2 0.8 I need to melt it in such a way that it looks like this: ID Cat Class Prob 1 Veg 1 0.9 1 Veg 2 0.1 2 Veg 1 0.8 2 Veg 2 0.2 3 Meat 1 0.6 3 Meat 2 0.4 4 Meat 1 0.3 4

Pivot table to “tidy” data frame in Pandas

六月ゝ 毕业季﹏ 提交于 2020-05-15 09:36:25
问题 I have an array of numbers (I think the format makes it a pivot table) that I want to turn into a "tidy" data frame. For example, I start with variable 1 down the left, variable 2 across the top, and the value of interest in the middle, something like this: X Y A 1 2 B 3 4 I want to turn that into a tidy data frame like this: V1 V2 value A X 1 A Y 2 B X 3 B Y 4 The row and column order don't matter to me, so the following is totally acceptable: value V1 V2 2 A Y 4 B Y 3 B X 1 A X For my first

How to further melt a dataset?

[亡魂溺海] 提交于 2020-05-05 12:42:10
问题 I'm working with the following data: #Reproducible Example Bills <- c("93-HCONRES-106", "93-HCONRES-107", "93-HCONRES-108") Members <- c("00134", "00416;00010;00017;00026", "00416;00503;00513;00568") data <- data.frame(Bills, Members) Where the data looks like this: #Data Structure Bills Members 1 93-HCONRES-106 00134 2 93-HCONRES-107 00416;00010;00017;00026 3 93-HCONRES-108 00416;00503;00513;00568 What I would like is for the dataset to extend, such that each bill corresponds with each

melt column by substring of the columns name in pandas (python)

 ̄綄美尐妖づ 提交于 2020-01-24 12:52:47
问题 I have dataframe: subject A_target_word_gd A_target_word_fd B_target_word_gd B_target_word_fd subject_type 1 1 2 3 4 mild 2 11 12 13 14 moderate And I want to melt it to a dataframe that will look: cond subject subject_type value_type value A 1 mild gd 1 A 1 mild fg 2 B 1 mild gd 3 B 1 mild fg 4 A 2 moderate gd 11 A 2 moderate fg 12 B 2 moderate gd 13 B 2 moderate fg 14 ... ... Meaning, to melt based on the delimiter of the columns name. What is the best way to do that? 回答1: One more approach