ik629
ik629

Reputation: 125

Append Dataframe in Pandas within a function

I'm using this function and APScheduler to add rows to a Dataframe everyhour. The problem is every time it runs it overwrites the previous entry so its not really "appending" anything.

def SMSx(frame):
    SMS(frame)
    frame = frame.append(SMS.SMSdf)
    frame = frame[frame.a != "test"]
    frame = DataFrame(frame, columns=['Time', 'Hour', 'Date', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'Total', 'Bucket'])
    frame.to_excel('Sprint_Log.xlsx', 'Sheet 1', index=False)
    SMSx.frame = frame

If I use the exact same code (below) and run it manually it works just fine. I'm not too sure what is going on here. SMS.SMSdf is a data frame from the SMS function.

SMS(frame)
frame = frame.append(SMS.SMSdf)
frame = frame[frame.a != "test"]
frame = DataFrame(frame, columns=['Time', 'Hour', 'Date', 'Day', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'Total', 'Bucket'])
frame.to_excel('Sprint_Log.xlsx', 'Sheet 1', index=False)
SMSx.frame = frame

Thank you for the help!

Code that worked:

def SMSx(frame_temp, frame_perm):
    SMS(frame_temp)
    try:
        frame_perm = DataFrame.from_csv('Sprint_Log.csv')
    except:
        pass
    frame_perm = frame_perm.append(SMS.SMSdf)
    frame_perm = frame_perm.drop_duplicates()
    frame_perm = frame_perm[frame_perm.Ready != "test"]
    frame_perm = DataFrame(frame_perm, columns=['Time', 'Hour', 'Date', 'Day', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'Total', 'Bucket'])
    frame_perm.to_csv('Sprint_Log.csv')
    SMSx.frame2 = frame_perm

Upvotes: 1

Views: 2038

Answers (1)

mfitzp
mfitzp

Reputation: 15545

The issue I suspect is that you do not return the updated frame variable. While you assign to the SMSx variable in the function scope, as soon as the function exits that is lost. I'm not sure how this is working however, since you do not first define the SMSx variable (it's the name of the current function, or is also a global variable?)

def SMSx(frame):
    SMS(frame)
    frame = frame.append(SMS.SMSdf)
    frame = frame[frame.a != "test"]
    frame = DataFrame(frame, columns=['Time', 'Hour', 'Date', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'Total', 'Bucket'])
    frame.to_excel('Sprint_Log.xlsx', 'Sheet 1', index=False)
    return frame


while True:
     frame = SMSx(frame)  # The returned frame will be used on the next iteration

Without seeing the rest of your code, it's pretty difficult to see what you're trying to do.

Upvotes: 1

Related Questions