python - Dictionary column in pandas dataframe -


i've got csv i'm reading pandas dataframe. 1 of columns in form of dictionary. here example:

cola, colb, colc, coldd 20, 30, {"ab":"1", "we":"2", "as":"3"},"string" 

how can turn dataframe looks this:

cola, colb, ab, we, as, coldd 20, 30, "1", "2", "3", "string" 

edit fixed question, looks string needs parsed, not dict object.

so starting 1 row df

    col   col b   col c                           col d 0   20      30      {u'we': 2, u'ab': 1, u'as': 3}  string1 

edit: based on comment op, i'm assuming need convert string first

import ast df["colc"] =  df["colc"].map(lambda d : ast.literal_eval(d)) 

then convert col c dict, transpose , join original df

dfnew = df.join(pd.dataframe(df["col c"].to_dict()).t) dfnew 

which gives this

    col   col b   col c                           col d   ab   0   20      30      {u'we': 2, u'ab': 1, u'as': 3}  string1 1   3   2 

then select columns want in dfnew

dfnew[["col a", "col b", "ab", "we", "as", "col d"]]      col   col b   ab    col d 0   20      30      1   2   3   string1 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -