1

I have a List of list values as shown below:

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

I have a data frame as shown below:

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

It'll look as shown below:

              labels memberid
        0       0       a1
        1       1       a2
        2       2       a3

I want to add another column called prob based on the labelscolumn, the value constituting from res list. The output will look as what you get when you run the below:

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


       labels memberid   prob
0       0       a1       a b b
1       1       a2       d e
2       2       a3       f g

So basically, I use the labels value as the index for res list and populate the prob column.

I have run the code below:

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

But I don't get the output that I want from the above code. What am I doing wrong?

1
  • please see my answer. The problem is, you keep reassigning df__["prob"] under the for/loop hence the last value wins Commented Nov 10, 2014 at 11:48

1 Answer 1

3

Re your error, it lies on this:

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

You keep reassigning df__["prob"] = 1 value, which is the latest " ".join(res[l]) Hence at the end the whole column is just the last value. To correct this, you can change to this:

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

Also you can use map and lambda, like this to achieve the same result, this is more efficient than your attempt:

import pandas as pd

df__ = pd.DataFrame({'memberid': ['a1','a2','a3'],
                   'labels':[0,1,2]})
res = [["a", "b", "b"], ["d", "e"], ["f", "g"]]
# you can map the values from '__labels' and feed to 'prob' with lambda
df__['prob'] = map(lambda x: ' '.join(res[x]), df__['labels'])

df__
   labels memberid   prob
0       0       a1  a b b
1       1       a2    d e
2       2       a3    f g
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much for this!!! :) It works! And thank you for showing the pandas way to do it too.
@user1452759, not a problem! glad it helps :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.