Thursday 15 March 2012

python - Pandas add new column based on another column -



python - Pandas add new column based on another column -

i have list of list values shown below:

res = [["a", "b", "b"], ["d", "e"], ["f", "g"]]

i have info frame shown below:

df__ = pd.dataframe({'memberid': ['a1','a2','a3'], 'labels':[0,1,2]})

it'll shown below:

labels memberid 0 0 a1 1 1 a2 2 2 a3

i want add together column called prob based on labelscolumn, value constituting res list. output when run below:

df__ = pd.dataframe({'memberid': ['a1','a2','a3'], 'labels':[0,1,2], labels memberid prob 0 0 a1 b b 1 1 a2 d e 2 2 a3 f g

so basically, utilize labels value index res list , populate prob column.

i have run code below:

for in range(len(df__["labels"])): k = df__.iloc[i]["labels"] df__["prob"] = " ".join(res[k])

but don't output want above code. doing wrong?

re error, lies on this:

df__["prob"] = " ".join(res[k])

you maintain reassigning df__["prob"] = 1 value, latest " ".join(res[l]) hence @ end whole column lastly value. right this, can alter this:

prob = [] in range(len(df__["labels"])): k = df__.iloc[i]["labels"] prob.append(" ".join(res[k])) df__['prob'] = prob

also can utilize map , lambda, accomplish same result, more efficient attempt:

import pandas pd df__ = pd.dataframe({'memberid': ['a1','a2','a3'], 'labels':[0,1,2]}) res = [["a", "b", "b"], ["d", "e"], ["f", "g"]] # can map values '__labels' , feed 'prob' lambda df__['prob'] = map(lambda x: ' '.join(res[x]), df__['labels']) df__ labels memberid prob 0 0 a1 b b 1 1 a2 d e 2 2 a3 f g

python pandas dataframes

No comments:

Post a Comment