Replacing value in csv using pandas isn't working

Question:

I’m working on a mock back end for an application; practicing OOP.

I’m trying to replace a value in a .csv table and everything seems to be going well until the end where the code simply doesnt change anything. I’ve tried debugging as shown further down and it shows the values im using are correct

    def test2(fname, lname, amount):
        # Open csv
        with open('clients.csv','r') as f:
            read = csv.reader(f)


            # Find index of client
            for index, row in enumerate(read):
                if fname in row[0]:
                    if lname in row[1]:
                        i = index - 1
            
            df = pd.read_csv('clients.csv')
            df.iat[i,6] = amount

using print(df.iloc[i,6]) gives me the value I want to change but using df.iat[i,6] = amount does nothing.

(Sorry if this seems messy, I’m new to asking questions on here!)

Asked By: yay

||

Answers:

    def test2(fname, lname, amount):
    # Open csv
    with open('clients.csv','r') as f:
        read = csv.reader(f)


        # Find row index of client
        for index, row in enumerate(read):
            if fname in row[0]:
                if lname in row[1]:
                     i = index - 1
                     

        # Update balance
        df = pd.read_csv('clients.csv')
        df.at[i,'Balance'] = amount
        df.to_csv('clients.csv', index=False)

is the solution

Answered By: yay
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.