Python Forum
Pandas DataFrame not updating
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pandas DataFrame not updating
#2
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)
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
However, once I put in into the menu loop it doesn't delete the row at all.

#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)
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
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?

I apologize for my lengthy thread and thank you for all the help. :)
Reply


Messages In This Thread
Pandas DataFrame not updating - by HelpMePlease - Jul-10-2020, 02:21 PM
RE: Pandas DataFrame not updating - by HelpMePlease - Jul-10-2020, 03:47 PM
RE: Pandas DataFrame not updating - by HelpMePlease - Jul-10-2020, 05:44 PM
RE: Pandas DataFrame not updating - by jefsummers - Jul-11-2020, 07:19 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Add NER output to pandas dataframe dg3000 0 233 Apr-22-2024, 08:14 PM
Last Post: dg3000
  HTML Decoder pandas dataframe column mbrown009 3 1,112 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Use pandas to obtain cartesian product between a dataframe of int and equations? haihal 0 1,159 Jan-06-2023, 10:53 PM
Last Post: haihal
  Pandas Dataframe Filtering based on rows mvdlm 0 1,484 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  Pandas dataframe: calculate metrics by year mcva 1 2,368 Mar-02-2022, 08:22 AM
Last Post: mcva
  Pandas dataframe comparing anto5 0 1,299 Jan-30-2022, 10:21 AM
Last Post: anto5
  PANDAS: DataFrame | Replace and others questions moduki1 2 1,856 Jan-10-2022, 07:19 PM
Last Post: moduki1
  PANDAS: DataFrame | Saving the wrong value moduki1 0 1,586 Jan-10-2022, 04:42 PM
Last Post: moduki1
  update values in one dataframe based on another dataframe - Pandas iliasb 2 9,421 Aug-14-2021, 12:38 PM
Last Post: jefsummers
  empty row in pandas dataframe rwahdan 3 2,501 Jun-22-2021, 07:57 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020