Issue
I have a list of lists. Here is an example of 2 of the lists inside a list:
global_tp_old = [[2, 1, 0.8333595991134644],[2, 1, 0.8530714511871338]]
I want to access a dataframe index where the index is specified in the first element of the above list in a list. At the moment I have tried:
global_tp_new = []
for element in global_tp_old:
element[:][0] = df_unique[element[:][0]]
global_tp_new.append(element)
where df_unique
is a pandas dataframe produced like this:
['img1.png', 'img2.png', 'img3.png']
I'm trying to match the first element from the list defined above to the number in df_unique
.
I should get:
'img3.png'
as it's the 3rd element (0 indexing)
However, I get the incorrect output where it essentially returns the first element every time. It's probably obvious but what do I do to fix this?
Solution
Remember that your element
array is actually a reference into the original list. If you modify the list, you'll modify global_tp_old
as well.
Something like this, although you may need to change the dataframe indexing depending on whether you're looking for rows or columns.
global_tp_old = [[2, 1, 0.8333595991134644],[2, 1, 0.8530714511871338]]
global_tp_new = []
for element in global_tp_old:
element = [df_unique.iloc[element[0]]] + element[1:]
global_tp_new.append(element)
Answered By - Tim Roberts
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.