How to read only the last lines by the time ID of an excel file in Pandas to perform calculations for them and write them to a separate file

Viewed 25

I am constantly downloading data from the DDE server of another program, which is then transferred to my Python application.

The DEE server transfers them to an excel file

They look something like this:

Time Value Qty V Operation OI
10:00:00 19320 3 57960 UP 187686
10:00:00 19325 2 57255 UP 187623
10:00:00 19326 10 57960 UP 187425
10:00:00 19310 5 57960 Down 187222
10:00:00 19355 2 57960 Down 187432
10:00:01 19333 1 57960 UP 187552
10:00:01 19310 5 57960 Down 187123
10:00:01 19312 2 57960 UP 188123
10:00:01 19344 9 57960 Down 188822

I run a lot of functions with a while loop and run my calculations with it

def Convert():
    b = 1
    while a == True:
        tic = time.perf_counter()
        Read_exel()
        Poc()
        Tik()
        Ct()
        Delta_By_Sel_Second()
        Open()
        Volume()
        Pmm()
        P_last()
        Comulativ_delta()
        Data_with_Oi()
        print('The cycle has been completed', b, 'times')
        print(" ")
        b = b+1
        toc = time.perf_counter()
        print(f"The calculation took {toc - tic:0.4f} seconds")
        time.sleep(10)

For example, here are a few of my functions

def Read_exel():
    Exel = pd.DataFrame(pd.read_excel(Ex))
    Exel.to_csv (Ex_Csv,
        index = False,
        header=True)


def Volume():
    A = ["S","T", "5T", "10T" ]
    for a in A:
            df = pd.DataFrame(pd.read_csv(Ex_Csv,  usecols=["Time", 'Qty'], parse_dates=[0]))
            df['Time'] = df['Time'].dt.floor(a, 0).dt.time
            #print(df)
            b = df.groupby('Time').agg('sum')
            if a == "S":
                b.to_csv(V_second)
            elif a == "T":
                b.to_csv(V_min)
            elif a == "5T":
                b.to_csv(V_Five)
            else:
                b.to_csv(V_Ten)

Everything is read by the Read_excel() function and written to csv, and then all the other calculations come from this csv file

Because of this, with each new iteration of the while loop, all files are overwritten in a new way

Is there any way to make excel read only the latest data with the latest time identifier, that is, if the last time in exel was 12:00:13, then he wrote all the rows with this identifier in csv

1 Answers
def Read_Ex():
    Exel = pd.DataFrame(pd.read_excel(Ex))
    Exel['Time'] = pd.to_datetime(Exel['Time']).dt.time
    A = Exel[Exel['Time'] == Exel['Time'].max()]
    A.to_csv (Ex_Csv,
        index = False,
        header=True)

Related