Home > Software engineering >  How to map two list of list together to one value in my dataframe?
How to map two list of list together to one value in my dataframe?

Time:12-21

I have two lists of lists that I am trying to map to another value in my dataframe.

Example of the list:

Potential_Cond_lst = [['Any Muscle Dis'], ['Type 2 fun','My happy place','Any Endo','Any Muscle Dis'],
 ['Mad people outiside','Ox tail','Hyper T','Wu Tang'],
 ['Type 2', 'Any Endo'],
 ['Other friends', 'Encounter for friends'],
 ['Any Endo', 'Any Muscle D', 'Major Frank'],
 ['Other friends', 'Any Muscle Disease'],.....]

Confidence_lvl_lst = [['50.8%'],
 ['96.3%', '94.1%', '94.0%', '61.5%'],
 ['99.0%', '99.0%', '93.6%', '45.5%'],
 ['99.0%', '89.4%'],
 ['70.0%', '31.5%'],
 ['92.6%', '70.7%', '20.0%'],
 ['88.1%', '59.2%'], ....]

I am trying to map these two list so they they would look like this:

Complete_lst = [['Any Muscle Dis': '50.8%'], 
['Type 2 fun': '96.3%','My happy place':'94.1%','Any Endo':'94.0%','Any Muscle Dis':'61.5%'], ['Mad people outiside': '99.0%','Ox tail':'99.0%','Hyper T':'93.6%','Wu Tang': '45.5%'], .....]

This way when I create a dataframe the output will look like this:



  
    ID              Reason                                                         Test Date of Reason                      Name of Test Done                            Potential Conditions with Confidence Level
0   87435   [Hanks Finger (11), Hanks left Finger (13), Hanks Right Finger (48]    2022-03-24                               [Hanks Finger (13), Hanks Left Finger (11)]  ([Any Muscle D: 50.8%])
1   49370   Franks and Beans (45)                                                  2022-07-05                               [Fransk and Beans (45)]                      ([Type 2 fun: 96.3%, My happy place:94.1% ,Any End: 94.0%, Any Muscle D: 61.5%])


CodePudding user response:

This should do it. To be clear, this will create a list of dictionaries not a list of lists, although, based off what you have written, I'm not sure which one of those you want. For a list of lists, change dict1 = {} to list1 = [] and dict1[val] = val1 to list1.append(val ':' val1)

Complete_lst = []
for lst in Potential_Cond_lst:
    for lst1 in Confidence_lvl_lst:
       dict1 = {}
       for val in lst:
          for val1 in lst1:
             dict1[val] = val1
       Complete_lst.append(dict1)

CodePudding user response:

You could use the 'zip' function and List Comprehension:

Complete_lst = [[k,v] for k,v in zip(Potential_Cond_lst, Confidence_lvl_lst)]

print(Complete_lst)

Output:

[[['Any Muscle Dis'], ['50.8%']], [['Type 2 fun', 'My happy place', 'Any Endo', 'Any Muscle Dis'], ['96.3%', '94.1%', '94.0%', '61.5%']], [['Mad people outiside', 'Ox tail', 'Hyper T', 'Wu Tang'], ['99.0%', '99.0%', '93.6%', '45.5%']], [['Type 2', 'Any Endo'], ['99.0%', '89.4%']], [['Other friends', 'Encounter for friends'], ['70.0%', '31.5%']], [['Any Endo', 'Any Muscle D', 'Major Frank'], ['92.6%', '70.7%', '20.0%']], [['Other friends', 'Any Muscle Disease'], ['88.1%', '59.2%']]]
  • Related