I created an excel (.csv) file with two columns – one for Movies I want to watch and one for Watched Movies. I an trying to write Python functions that can add movies to either list with some conditions. For example, if the movie is already in the list you’re trying to add it to, the user will be told that it is already in the list and the movie will not be added. Additionally, if the movie is not in the list you’re trying to add it to, but it is in the other list, the movie will be removed from that list and added to the one you want. I am using pandas to interact with the .csv file.
My function for adding to the Movies list works just fine. But for some reason the function that adds movies to the Watched Movies list only works the first time you use it. If you try to add a second movie to the Watched Movies list, the code runs (since the print statement at the end does display), but the movie does not get added to the list. I am really confused because the code in this function is exactly the same as the code in the function that works. Also, if I remove “ignore_index=True” in the pd.concat command, I get a ValueError saying “cannot reindex on an axis with duplicate labels”, but this does not happen if I remove the same bit from the other function.
If you want an extra challenge I would also like to figure out how to ensure that the Nan elements that appear when removing movies from a list are discarded and the indices reset to 0,1,2,3,4,etc. I know basically how to do this with .dropna(ignore_index=True), but then, if I move a movie from the longer list to the shorter list, the longer list gets cut down to match the length of the shorter list. This is the less important issue though.
I will include my code and functions below. The addmovie() function pretty much works, but the addwatched() function is the one that is broken. To test the code yourself, you will just need to create an excel (.csv) file, create column labels “Movies” and “Watched Movies”, and rename the variable “file” to the appropriate path on your own computer. I also included the function that prints out both columns for you to see.
import numpy as np
import pandas as pd
file = 'C:\Users\aosog\Downloads\Spyder\A_My_Python\Random\test.csv'
olddata = pd.read_csv(file, skiprows=1, names=['Movies', 'Watched Movies'])
movies = olddata['Movies']
watched = olddata['Watched Movies']
newdata = pd.DataFrame(columns=['Movies', 'Watched Movies'])
def addmovie(name):
'''
add movie to watch
name must be a string
'''
global olddata
global newdata
global movies
global watched
if name not in set(movies):
if name in set(watched):
yn = input(f'nYou have already watched "{name}"! nDo you still want to add it to your list of movies to watch? nDoing so will remove it from your watched movies. nny/n: ')
if yn=='y':
movies = pd.concat([movies, pd.Series(name)], ignore_index=True)
newdata['Movies'] = movies
newdata['Watched Movies'] = watched
newdata.to_csv(file)
watched = watched.drop(np.where(watched == name)[0])
newdata['Movies'] = movies
newdata['Watched Movies'] = watched
newdata.to_csv(file)
print(f'n"{name}" has been added to your movies and removed from watched movies.')
elif yn=='n':
print(f'''nOK. "{name}" will not be added to your movie list.''')
else:
print("nPlease enter 'y' for yes or 'n' for no.")
else:
movies = pd.concat([movies, pd.Series(name)], ignore_index=True)
newdata['Movies'] = movies
newdata['Watched Movies'] = watched
newdata.to_csv(file)
print(f'n"{name}" has been added to your list of movies to watch.')
else: print(f'n"{name}" is already in your list of movies to watch.')
def addwatched(name):
'''
add watched movie
name must be a string
'''
global olddata
global newdata
global movies
global watched
if name not in set(watched):
watched = pd.concat([watched, pd.Series(name)], ignore_index=True)
newdata['Movies'] = movies
newdata['Watched Movies'] = watched
newdata.to_csv(file)
print(f'n"{name}" has been added to your watched movies.')
if name in set(movies):
movies = movies.drop(np.where(movies == name)[0])
newdata['Movies'] = movies
newdata['Watched Movies'] = watched
newdata.to_csv(file)
print(f'n"{name}" has been removed from your watched movies.')
else: print(f'n"{name}" is already in your list of watched movies.')
def showmovies():
'''
shows all movies watched and unwatched
'''
olddata = pd.read_csv(file, skiprows=1, names=['Movies', 'Watched Movies'])
movies = olddata['Movies'].dropna(ignore_index=True)
watched = olddata['Watched Movies'].dropna(ignore_index=True)
try:
if len(max(movies, key=len))>6:
maxmovielen = len(max(movies, key=len))
else: maxmovielen = 6
except: maxmovielen = 6
try:
if len(max(watched, key=len))>7:
maxwatchedlen = len(max(watched, key=len))
else: maxwatchedlen = 7
except: maxwatchedlen = 7
print(f"n{'Movies':<{maxmovielen}} Watched")
print('_' * (maxmovielen + maxwatchedlen + 4))
if len(movies)>=len(watched):
maxlen = len(movies)
else: maxlen = len(watched)
for i in range(maxlen):
try: print(f"{movies[i]:<{maxmovielen}}", end=' ')
except: print(' ' * maxmovielen, end=' ')
try: print(watched[i])
except: print('')
So I expect that when I run addwatched(“blah”) that the message “”blah” has been added to your list of movies to watch.” will be shown and then when I run showmovies() I will see “blah” in the Watched Movies column. However, the message DOES display, but “blah” does not get added to the column. The code block that should accomplish this is exactly the same as the code block in the addmovies() function that DOES work…but somehow it doesn’t work in the addwatched() function…
Austin Osogwin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.