简体   繁体   中英

Pandas add new column based on another column

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 labels column, 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM