Saturday, 15 May 2010

python - Mapping pandas DataFrame rows to a pandas Series -



python - Mapping pandas DataFrame rows to a pandas Series -

i need create pandas series elements each function of row dataframe. 'metadata' column json string , want series of dicts json plus rest of columns. ideally want equivalent map method dataframe:

df.map(lambda row: json.loads(row.metadata).update({'timestamp':row.timestamp}))

(update destructive , not homecoming new dict point)

edit: can re-create this

metadata timestamp "{'a':1,'b':2}" 000000001 "{'a':1,'c':2}" 000000002 "{'a':1,'c':2}" 000000003

and load

in [8]: import pandas pd in [9]: pd.read_clipboard() out[9]: metadata timestamp 0 {'a':1,'b':2} 1 1 {'a':1,'c':2} 2 2 {'a':1,'c':2} 3

the desired result should pandas.series contents of list:

[{"a":1,"b":2,"timestamp":000000001} {"a":1,"c":2,"timestamp":000000002} {"a":1,"c":2,"timestamp":000000003}]

what modify strings? like:

new_metadata = df.apply(lambda x: '{}\b,"timestamp":{}}}'.format(x.metadata,x.timestamp),axis=1)

which produces:

in [1]: new_metadata out[2]: 0 {'a':1,'b':2,"timestamp":1} 1 {'a':1,'c':2,"timestamp":2} 2 {'a':1,'c':2,"timestamp":3}

python pandas

No comments:

Post a Comment