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?
df__["prob"]under thefor/loophence the last value wins