Create a new DataFrame called Combo that contains every unique combination of characters, bodies, and tires.

QUESTION

Python code (use for loop)

character:A, B

body:C, D

tire:E, F

Create a new DataFrame called Combo that contains every unique combination of characters, bodies, and tires. This DataFrame should have three columns, ‘Character’, ‘Body’, and ‘Tire’, and should have 8 rows. There are 2 characters, 2 bodies, and 2 tires, so that means there are 8 unique combinations.(EX: ACE, ACF)

Once you have created your Combo dataframe, perform a multilevel sort first by Character, then by Body, then by Tire (all in ascending order). Make sure to actually change the Combo dataframe when you perform the sort (in other words, perform the sort in-place).

Lastly, once you have sorted the Combo dataframe, reset the index for the newly sorted dataframe.

 

SUMMARY

Character body and tire contents are taken as input from the user. Each combination, a character, body, and tire is inserted in the combo data frame. This combo is then sorted first based on character, then the body, and then the last based on the tire.

 

EXPLANATION

For a new data frame named Combo, a new list is created. The character, body, and tire are then declared and assigned with the user input. Each of the characters is separated by a comma since the character, body and tire are in string format. Once we are done splitting the character, tire and body we store the characters in them in a list. For every character, every possible combo of body and tire is attached. For everybody’s character possible, the tire character is attached. In similar combinations, every character, body, and tire are stored as strings and put into the data frame named Combo. The contents in the data frame are sorted using the bubble sort technique. Since in a string is the first character are same, then automatically while checking the next preference would be the second character of both the strings. So, sorting will be first based on ascending order of character, then the ascending of body, and then the tire. 

 

CODE

# new data frame 
Combo=[]
c=input("character: ")
b=input("body: ")
t=input("tire: ")
c=list(c.split(','))
b=list(b.split(','))
t=list(t.split(','))
# body and tire in the combo 
for i in c:
    for j in b:
        for k in t:
            s=i+j+k 
            Combo.append(s)
print("Initial Combo:",Combo)
# sorting the combo 
for i in range(len(Combo)):
    for j in range(len(Combo)-1-i):
        if Combo[j]>Combo[j+1]:
            temp=Combo[j]
            Combo[j]=Combo[j+1]
            Combo[j+1]=temp;
 
print("Sorted Combo:",Combo)

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *