如何使用 python 的 pandas 和 numpy 库自动将数据存储在新行中

我想将数据存储在 CSV 文件的第二行。但它每次都会覆盖第一行。怎么做?我使用循环在第二行存储数据。但它没有发生。我有什么想念的吗?


  import pandas as pd

    import numpy as np

    a=1

    def table_driven_agent(percept):

        location=['A','B']

        status=['clean','dirty']

        percepts=[]

        table=[location,status]

        percepts.append(percept)

        action = tableDrivenVaccum(percept,table)

        if action == 'dirty':

          action = dirty(percept[1])

        return(action)

    def tableDrivenVaccum(percept,table):

        for i in table:

            if i[0] == percept[0]:

                return(i[1])

    def dirty(percept):

        if percept == 'A':

            return('Go to B')

        elif percept =='B':

            return('Go to A')

    while( a<10):

        percept = list(map(str,input("Enter the status and position:").split(',')))

        action = table_driven_agent(percept)

        print(action)

        dict = {'action': action, 'percept': percept}

        df= pd.DataFrame(dict) 

        df.to_csv(r'index.csv', index =False, header=True)

        a=1

http://img1.mukewang.com/6412cda4000147bf03360191.jpg

肥皂起泡泡
浏览 85回答 2
2回答

喵喔喔

写入 csv 会创建一个新的 csv 并替换当前的 csv。这就是它写入第一行的原因:它实际上删除了前一行并写入空白文件。您正在寻找的是附加df到当前 csv 中。这可以通过以下方式完成:df.to_csv('index.csv',&nbsp;index=False,&nbsp;mode='a',&nbsp;header=True)注意我添加了mode='a'。这意味着模式=追加。默认mode为'w', 表示“写入”(覆盖)。这种方式df被添加到当前 csv 的最后一行。如果您的 csv 有超过 2 行并且您只想更改第二行,您应该首先将所有 csv 加载到数据框。然后你可以只更改第二行 (&nbsp;df.loc[1,:]=...) 然后将它全部保存到 csv (&nbsp;df.to_csv(r'index.csv', index =False, header=True)

慕盖茨4494581

好的,我自己解决了这个问题,方法是读取旧的 CSV 文件并将其合并并放入 CSV 文件中。你必须像我一样创建 CSV 文件 index.csv。然后将列名作为百分比和操作来工作。如果您的代码位置中没有 CSV 文件,将会出错。它会自动合并 CSV 文件中的最新输入而不删除任何行。注意:index.csv、百分比和操作只是一个示例。你也可以使用你的。import pandas as pdimport numpy as npi = 1def table_driven_agent(percept):&nbsp; &nbsp; location=['A','B']&nbsp; &nbsp; status=['clean','dirty']&nbsp; &nbsp; percepts=[]&nbsp; &nbsp; table=[location,status]&nbsp; &nbsp; percepts.append(percept)&nbsp; &nbsp; action = tableDrivenVaccum(percept,table)&nbsp; &nbsp; if action == 'dirty':&nbsp; &nbsp; &nbsp; action = dirty(percept[1])&nbsp; &nbsp; return(action)def tableDrivenVaccum(percept,table):&nbsp; &nbsp; for i in table:&nbsp; &nbsp; &nbsp; &nbsp; if i[0] == percept[0]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return(i[1])def dirty(percept):&nbsp; &nbsp; if percept == 'A':&nbsp; &nbsp; &nbsp; &nbsp; return('Go to B')&nbsp; &nbsp; elif percept =='B':&nbsp; &nbsp; &nbsp; &nbsp; return('Go to A')while i < 6:&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; percept = list(map(str,input("Enter the status and position:").split(',')))&nbsp; &nbsp; action = table_driven_agent(percept)&nbsp; &nbsp; print(action)&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; df_old =pd.read_csv('index.csv')&nbsp; &nbsp; newData=[[percept,action]]&nbsp; &nbsp; colNames =df_old.columns&nbsp; &nbsp; df_new = pd.DataFrame(data=newData, columns=colNames)&nbsp; &nbsp; df_complete = pd.concat([df_old,df_new],axis=0)&nbsp; &nbsp; df_complete.to_csv('index.csv',index=False)&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; #dict = {'percept': percept, 'action': action}&nbsp; &nbsp; #df= pd.DataFrame(dict)&nbsp;&nbsp; &nbsp; #df.to_csv(r'index.csv', index =False, header=True,mode='a')&nbsp; &nbsp; i =1
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python