Pandas wide_to_long() or alternative

30 views
Skip to first unread message

Adam Bernier

unread,
Jul 14, 2016, 2:43:49 PM7/14/16
to PyData
Having trouble with pandas wide_to_long(). Specifically not able to figure out how to get "ENCS", "OPEN", "no_dx" into a column. Alternate solutions welcome; perhaps using stack(), unstack().

     MO AREA      COVGS PROV_CAT               SPECIALTY  ENCS  OPEN  no_dx
100   1   AV  DHMO HDHP       MD     Hematology/Oncology     8     0      0
101   1   AV  DHMO HDHP       MD     Infectious Diseases     5     0      0
102   1   AV  DHMO HDHP       MD       Internal Medicine   329     0      0
103   1   AV  DHMO HDHP       MD              Nephrology     3     0      0
104   1   AV  DHMO HDHP       MD               Neurology    16     0      0
105   1   AV  DHMO HDHP       MD  Obstetrics, Gynecology   165     0      0
106   1   AV  DHMO HDHP       MD           Ophthalmology    41     0      0
107   1   AV  DHMO HDHP       MD  Orthopaedics, Medicine    39     0      0
108   1   AV  DHMO HDHP       MD          Otolaryngology    23     0      0
109   1   AV  DHMO HDHP       MD         Pain Management    14     0      0

Anyone able to assist? Thanks very much.


Adam Bernier

unread,
Jul 14, 2016, 2:53:35 PM7/14/16
to PyData
Ok I am able to do what I want using pandas melt:

pd.melt(hdf,id_vars=['COVGS','PROV_CAT'],value_vars=['ENCS','OPEN','no_dx'])
109   1   AV  DHMO HDHP       MD         Pain Management    14     0      0Enter code here...

Reply all
Reply to author
Forward
0 new messages