reshape

Split column name and convert data from wide to long format in R

百般思念 提交于 2021-01-27 05:14:32
问题 I have a large dataset that I need to convert to long format from wide format. That should be simple enough and there are lots of examples of how to do that on this forum. However, in this case, I also need to split the column headers used in the wide format and create a column for each of them in the long format. Example dataset data <- data.frame("East2010"=1:3, "West2010"=4:6, "East2011"=7:9, "West2011"=5:7) data East.2010 West.2010 East.2011 West.2011 1 1 4 7 5 2 2 5 8 6 3 3 6 9 7 What I

Reshape in R with variable name patterns

倖福魔咒の 提交于 2021-01-01 13:08:45
问题 I am trying to reproduce the results of a reshape in Stata using base R's reshape function. Stata webuse reshape3, clear li, clean // reshape long reshape long inc@r ue, i(id) j(year) list, sepby(id) clean This produces, before the reshape : . li, clean id sex inc80r inc81r inc82r ue80 ue81 ue82 1. 1 0 5000 5500 6000 0 1 0 2. 2 1 2000 2200 3300 1 0 0 3. 3 0 3000 2000 1000 0 0 1 Note the pattern of the names for the stub inc . After the reshape , I get: . list, sepby(id) clean id year sex incr

Reshape in R with variable name patterns

久未见 提交于 2021-01-01 12:50:53
问题 I am trying to reproduce the results of a reshape in Stata using base R's reshape function. Stata webuse reshape3, clear li, clean // reshape long reshape long inc@r ue, i(id) j(year) list, sepby(id) clean This produces, before the reshape : . li, clean id sex inc80r inc81r inc82r ue80 ue81 ue82 1. 1 0 5000 5500 6000 0 1 0 2. 2 1 2000 2200 3300 1 0 0 3. 3 0 3000 2000 1000 0 0 1 Note the pattern of the names for the stub inc . After the reshape , I get: . list, sepby(id) clean id year sex incr

Python DataFrame: transpose one column into multiple column

痞子三分冷 提交于 2020-12-12 14:17:29
问题 I have a dataframe like below: df = pd.DataFrame({'month':['2017-09-27','2017-09-27','2017-09-28','2017-09-29'],'Cost':[100,500,200,300]}) How can I get a df like this: 2017-09-27 2017-09-28 2017-09-29 100 200 300 500 NULL NULL Thanks in advance! 回答1: Use cumcount to compute a "cumulative count" of the items within each group. We'll use these values (below) as index labels. In [97]: df['index'] = df.groupby('month').cumcount() In [98]: df Out[98]: Cost month index 0 100 2017-09-27 0 1 500

Python DataFrame: transpose one column into multiple column

霸气de小男生 提交于 2020-12-12 14:15:06
问题 I have a dataframe like below: df = pd.DataFrame({'month':['2017-09-27','2017-09-27','2017-09-28','2017-09-29'],'Cost':[100,500,200,300]}) How can I get a df like this: 2017-09-27 2017-09-28 2017-09-29 100 200 300 500 NULL NULL Thanks in advance! 回答1: Use cumcount to compute a "cumulative count" of the items within each group. We'll use these values (below) as index labels. In [97]: df['index'] = df.groupby('month').cumcount() In [98]: df Out[98]: Cost month index 0 100 2017-09-27 0 1 500

Pandas - combine row dates with column times

半腔热情 提交于 2020-12-11 08:57:52
问题 I have a dataframe: Date 0:15 0:30 0:45 ... 23:15 23:30 23:45 24:00 2004-05-01 3.74618 3.58507 3.30998 ... 2.97236 2.92008 2.80101 2.6067 2004-05-02 3.09098 3.84625 3.54672 ... 2.83725 2.93876 2.82762 2.6255 How do I convert it to: Date value 2004-05-01 0:15 3.74618 2004-05-01 0:30 3.58507 2004-05-01 0:45 3.30998 ... I wrote some code which does work, but I'm sure it's possible to do the same in more elegant couple of lines code cols = [] for col in frame.columns.values: if col != '24:00': dt