user4599
user4599

Reputation: 127

create new column in dataframe conditionally

updated question

by using the code below i am able to access dataframe only after completion of for loop, but i want to use most recently created column of the dataframe at intermediate time. i.e after every 5 minutes whichever is the last column of the dataframe ,how to achieve this?

@app.route("/sortbymax")
def sortbymax():
    df = updated_data()
    #### here i want to use most recently created column
    df = create_links(df)
    df = df.sort_values(by=['perc_change'], ascending=False)
    return render_template('sortbymax.html',tables=[df.to_html(escape = False)], titles=df.columns.values)


def read_data():
    filename = r'c:\Users\91956\Desktop\bk.xlsm'
    df = pd.read_excel(filename)    
    return df


def updated_data():
    df = read_data()
    for i in range(288): 
        temp = read_data()
        x=datetime.datetime.now().strftime("%H:%M:%S")
        df['perc_change_'+x] = temp['perc_change']
        time.sleep(300)
    return df

Upvotes: 0

Views: 79

Answers (1)

IoaTzimas
IoaTzimas

Reputation: 10624

I see you have a file .xlsm which means is a macro enabled excel. I guess you can read it but if you want to change it with python than you most probably lose the macro part in your excel.

For the python part: this will copy the perc_change column every 5 minutes, with the respective name. However bear in mind that this will work only for one day (it will replace existing columns after that). If you want to work for longer periods, let me know so that I will add day-month-year (whatever you want) in column names.

import datetime
import time

def read_data():
    filename = r'c:\Users\91956\Desktop\bk.xlsm'
    df = pd.read_excel(filename)    
    return df

def write_data(df):
    filename = r'c:\Users\91956\Desktop\bk.xlsm'
    df.to_excel(filename)    
    
df = read_data() #read excel for first time

for i in range(288): #this will run for one day exactly
    temp = read_data()
    x=datetime.datetime.now().strftime("%H:%M")
    df['perc_change_'+x] = temp['perc_change']
    time.sleep(300)

Upvotes: 2

Related Questions