I am running into a problem when using python to add and remove items from a csv file

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…

New contributor

Austin Osogwin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật