Jul-10-2020, 05:44 PM
(Jul-10-2020, 03:47 PM)HelpMePlease Wrote: I separated this part from my main post as it is a different problem I'm facing regarding the deletion of rows from the DataFrame.
The program is supposed to delete the car's whole row of data if the user inputs it in the deleteOption. I managed to get it to work individually as shown below.
import pandas as pd carList = [['Toyota Prius', 'MPV', 'Hybrid', '140000'], ['Honda City', 'Saloon', 'Economical', '75000'], ['Subaru Forester', 'SUV', 'Adventure', '140000'], ['Honda Jazz', 'Hatchback', 'Compact', '70000'], ['Tesla Model 3', 'Coupe', 'Electric Vehicle', '120000'], ['Mercedes-Benz', 'Cabriolet', 'Turbo-charged', '250000']] df = pd.DataFrame(carList, columns =['Model', 'Type', 'Spec', 'Price']) print(df) deleteOption=eval(input("Which car do you want to delete?: ")) df.drop(deleteOption)However, once I put in into the menu loop it doesn't delete the row at all.
Output:Model Type Spec Price 0 Toyota Prius MPV Hybrid 140000 1 Honda City Saloon Economical 75000 2 Subaru Forester SUV Adventure 140000 3 Honda Jazz Hatchback Compact 70000 4 Tesla Model 3 Coupe Electric Vehicle 120000 5 Mercedes-Benz Cabriolet Turbo-charged 250000 Which car do you want to delete?: 1 Model Type Spec Price 0 Toyota Prius MPV Hybrid 140000 2 Subaru Forester SUV Adventure 140000 3 Honda Jazz Hatchback Compact 70000 4 Tesla Model 3 Coupe Electric Vehicle 120000 5 Mercedes-Benz Cabriolet Turbo-charged 250000
#Display Main Menu print('*'*100+'\n'+' '*44+'PRACTICAL\n'+' '*44+'MAIN MENU\n'+ '1. Display List of Cars\n' + '2. Add Car\n' + '3. Edit Car\n' + '4. Delete Car\n'+ '\nEnter q to quit program\n'+'*'*100) #Set an initial value for choice. userinput = '' #Start a loop that runs until the user enters the value 'quit' to exit the program. while userinput != 'q': userinput = input("\nWhat would you like to do? ") #If user chooses 1 to Display Car List if userinput == '1': print("\nYou have decided to display the list of cars.") print("\n") #Import pandas as pd import pandas as pd carList = [['Toyota Prius', 'MPV', 'Hybrid', '140000'], ['Honda City', 'Saloon', 'Economical', '75000'], ['Subaru Forester', 'SUV', 'Adventure', '140000'], ['Honda Jazz', 'Hatchback', 'Compact', '70000'], ['Tesla Model 3', 'Coupe', 'Electric Vehicle', '120000'], ['Mercedes-Benz', 'Cabriolet', 'Turbo-charged', '250000']] df = pd.DataFrame(carList, columns =['Model', 'Type', 'Spec', 'Price']) print(df) print('*'*100) #If user chooses 4 to Delete Car elif userinput == '4': print("\nYou have decided to delete a car") print(df) deleteOption=eval(input("Which car do you want to delete?: ")) df.drop(deleteOption)Why doesn't the DataFrame with the deleted row get displayed at all in the menu loop although the functions are exactly the same? And how can I make it so that the DataFrame will always get updated when the user decides to display the list of cars at the end as well like my main problem?
Output:**************************************************************************************************** PRACTICAL MAIN MENU 1. Display List of Cars 2. Add Car 3. Edit Car 4. Delete Car Enter q to quit program **************************************************************************************************** What would you like to do? 1 You have decided to display the list of cars. Model Type Spec Price 0 Toyota Prius MPV Hybrid 140000 1 Honda City Saloon Economical 75000 2 Subaru Forester SUV Adventure 140000 3 Honda Jazz Hatchback Compact 70000 4 Tesla Model 3 Coupe Electric Vehicle 120000 5 Mercedes-Benz Cabriolet Turbo-charged 250000 **************************************************************************************************** What would you like to do? 4 You have decided to delete a car Model Type Spec Price 0 Toyota Prius MPV Hybrid 140000 1 Honda City Saloon Economical 75000 2 Subaru Forester SUV Adventure 140000 3 Honda Jazz Hatchback Compact 70000 4 Tesla Model 3 Coupe Electric Vehicle 120000 5 Mercedes-Benz Cabriolet Turbo-charged 250000 Which car do you want to delete?: 1
I apologize for my lengthy thread and thank you for all the help. :)
I HAVE SOLVED THE DELETION OF THE ROWS!! PLEASE SKIP THIS