python - Pandas Melt Function -


i have dataframe:

df = pd.dataframe([[2, 4, 7, 8, 1, 3, 2013], [9, 2, 4, 5, 5, 6, 2014]], columns=['amy', 'bob', 'carl', 'chris', 'ben', 'other', 'year'])     amy  bob  carl  chris  ben  other  year 0    2    4     7      8    1      3  2013 1    9    2     4      5    5      6  2014 

and dictionary:

d = {'a': ['amy'], 'b': ['bob', 'ben'], 'c': ['carl', 'chris']} 

i reshape dataframe this:

    group   name  year  value  0         amy  2013      2  1         amy  2014      9  2      b    bob  2013      4  3      b    bob  2014      2  4      b    ben  2013      1  5      b    ben  2014      5  6      c   carl  2013      7  7      c   carl  2014      4  8      c  chris  2013      8  9      c  chris  2014      5 10  other         2013      3 11  other         2014      6 

note other doesn't have values in name column , order of rows not matter. think should using melt function examples i've come across aren't clear.

melt gets part way there.

in [29]: m = pd.melt(df, id_vars=['year'], var_name='name') 

this has except group. that, need reshape d bit well.

in [30]: d2 = {}  in [31]: k, v in d.items():     item in v:         d2[item] = k    ....:  in [32]: d2 out[32]: {'amy': 'a', 'ben': 'b', 'bob': 'b', 'carl': 'c', 'chris': 'c'}  in [34]: m['group'] = m['name'].map(d2)  in [35]: m out[35]:     year   name  value group 0   2013    amy      2     1   2014    amy      9     2   2013    bob      4     b 3   2014    bob      2     b 4   2013   carl      7     c ..   ...    ...    ...   ... 7   2014  chris      5     c 8   2013    ben      1     b 9   2014    ben      5     b 10  2013  other      3   nan 11  2014  other      6   nan  [12 rows x 4 columns] 

and moving 'other' name group

in [8]: mask = m['name'] == 'other'  in [9]: m.loc[mask, 'name'] = ''  in [10]: m.loc[mask, 'group'] = 'other'  in [11]: m out[11]:     year   name  value  group 0   2013    amy      2      1   2014    amy      9      2   2013    bob      4      b 3   2014    bob      2      b 4   2013   carl      7      c ..   ...    ...    ...    ... 7   2014  chris      5      c 8   2013    ben      1      b 9   2014    ben      5      b 10  2013             3  other 11  2014             6  other  [12 rows x 4 columns] 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -