如何在同一数据帧上频繁更新值

2024-04-27 00:29:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我每天都从文本文件中读取数据

在这里,我讨论了定义星期一和星期二的两个循环。如果我在星期一启动程序,我可以在数据框中获取数据并进行更新,当一天移到星期二到星期五时,我可以在单个数据框中获取所有日期的信息

让我假设周一是2月4日,周五是2月8日

同样,当我在2月11日(星期一)运行程序时,更新的值应该只保存在星期一数据帧中。怎么做

newDF = pd.DataFrame()
new1=pd.DataFrame()
a=time.ctime()
b=a.split(" ")
print(a)
with open("call_1_feb.txt","r")as f:
    file=f.readlines()

if b[0]=="Mon":
        #print(b[1])
        Bcount,BPcount,BFcount,BOcount=0,0,0,0
        Scount,SPcount,SFcount,SOcount=0,0,0,0         
        for j in range(len(file)):
            if re.search("WIN",file[j]):
                Bcount+=1
                if re.search("\+\+",file[j]):
                    BPcount+=1   
                if re.search("--",file[j]):
                    BFcount+=1     
                if not re.search("\+\+",file[j]):
                    if not re.search("--",file[j]):
                        BOcount+=1

            if re.search("LOST",file[j]):
                Scount+=1
                if re.search("\+\+",file[j]):
                    SPcount+=1   
                if re.search("--",file[j]):
                    SFcount+=1   
                if not re.search("\+\+",file[j]):
                    if not re.search("--",file[j]):
                        SOcount+=1
        Date=b[0]+"["+b[1]+"-"+b[3]+"-"+b[5]+"]"
        #print(Date)
        T1=[Date,BPcount,BFcount,BOcount,Bcount,SPcount,SFcount,SOcount,Scount]
        df=pd.DataFrame(np.array(T1).reshape(-1,len(T1)))
        df=df.loc[0:]
        print(df)
        df.to_csv("output.csv")

if b[0]=="Tue":
            Bcount,BPcount,BFcount,BOcount=0,0,0,0
            Scount,SPcount,SFcount,SOcount=0,0,0,0         
            for j in range(len(file)):
                if re.search("BUY",file[j]):
                    Bcount+=1
                    if re.search("\+\+",file[j]):
                        BPcount+=1   
                    if re.search("--",file[j]):
                        BFcount+=1     
                    if not re.search("\+\+",file[j]):
                        if not re.search("--",file[j]):
                            BOcount+=1

                if re.search("SELL",file[j]):
                    Scount+=1
                    if re.search("\+\+",file[j]):
                        SPcount+=1   
                    if re.search("--",file[j]):
                        SFcount+=1   
                    if not re.search("\+\+",file[j]):
                        if not re.search("--",file[j]):
                            SOcount+=1
            Date1=b[0]+"["+b[1]+"-"+b[3]+"-"+b[5]+"]"
            T2=[Date1,BPcount,BFcount,BOcount,Bcount,SPcount,SFcount,SOcount,Scount]
            df1=pd.DataFrame(np.array(T2).reshape(-1,len(T2)))
            df1=df1.loc[0:]            
            new1 = new1.append(df, ignore_index = True)
            new1=new1.append(df1,ignore_index=True)
            print(new1)
            new1.to_csv("output.csv")

输入文件将是:

++INDIA WIN@769.6 --UAE WIN@1143.95 ++SRILANKA LOST@325.4 BANGLADESH WIN@19316.199` ++SCOTLAND WIN@160.8 AUSTRALIA LOST@451.85 USA WIN@2791.25 --UK LOST@661.85 KENYA WIN@942.75 --PAKISTAN LOST@1043.85,


Tags: researchifnotwinfilenew1bcount