Python Forum
Pandas DataFrame not updating
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pandas DataFrame not updating
#1
Hi, I am trying to program a Car Application menu loop to display the list of cars and add on cars according to the userinput 1 and 2 respectively.

In the end ,after the user adds on as many cars as they want and decides to display the list of cars once again by inputting 1, the list displayed should include the additional cars they added.


#Display Main Menu
print('*'*100+'\n'+' '*44+'PRACTICAL\n'+' '*44+'MAIN MENU\n'+
    '1. Display List of Cars\n' + 
    '2. Add 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 2 to Add Car       
    elif userinput == '2':
        print("\nYou have decided to add a car.")
        newmodel=input("What is the model of the new car? ")
        newtype=input("What is the type of the new car? ")
        newspec=input("What is the spec of the new car? ")
        newprice=input("What is the price of the new car? ")
        print("\n")
        to_append = [str(newmodel),str(newtype),str(newspec),str(newprice)]
        df_length = len(df)
        df.loc[df_length] = to_append
        print(df)
        print('*'*100)

    #If user chooses q to Quit Program        
    elif userinput == 'q':
        print("\nThis is the end of the Car Application.\n")
However, the list is not updated at the end as shown. I am unsure as to why is this so.

Output:
**************************************************************************************************** PRACTICAL MAIN MENU 1. Display List of Cars 2. Add 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? 2 You have decided to add a car. What is the model of the new car? BMW What is the type of the new car? SUV What is the spec of the new car? Economical What is the price of the new car? 135000 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 6 BMW SUV Economical 135000 **************************************************************************************************** What would you like to do? 2 You have decided to add a car. What is the model of the new car? Honda What is the type of the new car? Hatchback What is the spec of the new car? Compact What is the price of the new car? 102000 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 6 BMW SUV Economical 135000 7 Honda Hatchback Compact 102000 **************************************************************************************************** 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 ****************************************************************************************************
I would really appreciate any help. Thank you for your time!!
Reply
#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
#3
(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)
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. :)

I HAVE SOLVED THE DELETION OF THE ROWS!! PLEASE SKIP THIS
Reply
#4
Glad you fixed the delete problem.

The add problem is that you recreate the dataframe with the original data each time the user chooses "1". Might want to push that to the top
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  HTML Decoder pandas dataframe column mbrown009 3 961 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,090 Jan-06-2023, 10:53 PM
Last Post: haihal
  Pandas Dataframe Filtering based on rows mvdlm 0 1,396 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  Pandas dataframe: calculate metrics by year mcva 1 2,266 Mar-02-2022, 08:22 AM
Last Post: mcva
  Pandas dataframe comparing anto5 0 1,242 Jan-30-2022, 10:21 AM
Last Post: anto5
  PANDAS: DataFrame | Replace and others questions moduki1 2 1,758 Jan-10-2022, 07:19 PM
Last Post: moduki1
  PANDAS: DataFrame | Saving the wrong value moduki1 0 1,525 Jan-10-2022, 04:42 PM
Last Post: moduki1
  update values in one dataframe based on another dataframe - Pandas iliasb 2 9,099 Aug-14-2021, 12:38 PM
Last Post: jefsummers
  empty row in pandas dataframe rwahdan 3 2,417 Jun-22-2021, 07:57 PM
Last Post: snippsat
Question Pandas - Creating additional column in dataframe from another column Azureaus 2 2,913 Jan-11-2021, 09:53 PM
Last Post: Azureaus

Forum Jump:

User Panel Messages

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