pandas - Merge multiple columns into one column list with [key:value] combination in Python -


let me preface question noting combined column not dictionary. resulting dataframe has square brackets within 'combined' column - appears list within dataframe int format [key1:value1, key2:value2, etc].

i'm trying convert dataframe this:

import pandas pd test = pd.dataframe({'apples':['red','green','yellow'], 'quantity': [1,2,3],'tastefactor':['yum','yum','yuck']})     apples  quantity tastefactor 0     red         1         yum 1   green         2         yum 2  yellow         3        yuck 

to format, combining keys values in each row new column:

   apples  quantity tastefactor  combined 0     red         1         yum  ['apples':'red','quantity':'1','tastefactor':'yum'] 1   green         2         yum  ['apples':'green','quantity':'2','tastefactor':'yum'] 2  yellow         3        yuck  ['apples':'yellow','quantity':'3','tastefactor':'yuck'] 

tried turn dataframe dictionary per row, got stuck converting list.

test['combined'] = test.to_dict(orient='records') 

the resulting new column doesn't need actual list type. string.

previously asked question here wanted clarify question in title in question. how create list dictionary within dataframe in python

found following closely related questions , tried derivations of them gets me half way can't seem right format.

you can using apply method of pandas dataframes

import pandas pd df = pd.dataframe({'apples':['red','green','yellow'], 'quantity': [1,2,3],'tastefactor':['yum','yum','yuck']})  col_names = df.columns  def func(row):     global col_names     list_ = [str(b)+':'+str(a) a,b in zip(row,col_names.values.tolist())]     return list_  x = list(map(func, df.values.tolist())) df.loc[:,'combined'] = pd.series(x) # df #    apples  quantity tastefactor                                       combined # 0     red         1         yum      [apples:red, quantity:1, tastefactor:yum] # 1   green         2         yum    [apples:green, quantity:2, tastefactor:yum] # 2  yellow         3        yuck  [apples:yellow, quantity:3, tastefactor:yuck] 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -