简体   繁体   English

使用 Pandas df 存储用户输入,同时使用 Kivy 和 Python

[英]Using Pandas df to store User Input while using Kivy and Python

I do need to store User input in a Pandas Dataframe.我确实需要将用户输入存储在 Pandas Dataframe 中。 While launching the Programm the df should be empty.启动程序时,df 应该是空的。 on every screen there will be a new df generated and should be added ( concat for example ) to the main df.在每个屏幕上都会生成一个新的 df,应该将其添加(例如 concat)到主 df。 So i need to make the df and all changes availabe as a local variable in any screen.所以我需要在任何屏幕上将 df 和所有更改作为局部变量。

maindf= pd.DataFrame()

now on the next screen:现在在下一个屏幕上:

class screen1(Screen):
    def redo(self):
        pass
    def press(self):
        
        maindf['col'] = pd.Series([ 'A'for x in range(len(Folgenzähler))])
        pass

on the next screen i want to do the following:在下一个屏幕上,我想执行以下操作:

 class screen2(Screen):
    def press(self):
        values = []
        row = self.ids.Peter
        for row in row.children:
            for ch in reversed(row.children):
                if isinstance(ch, TextInput):
                    a = ch.text
                    print("Row 1 ist"+str(a))
                    values.append(str(a))
        
        hilf2 = copy.deepcopy(values)
        hilf2 = ' '.join(hilf2).split()
        hilf2 = np.array(hilf2).reshape(len(Folgenzähler), 3)

        datatoappend= pd.DataFrame(hilf2, columns=['A', 'B', 'C'])
        maindf= pd.concat([maindf, datatoappend], axis=1)

        print("Pandas maindf")
        print(maindf)

Afterwards the printed DF (maindf )should contain the columns ['col','A','B','C'] and their user generated Input Values.之后打印的 DF (maindf) 应该包含列 ['col','A','B','C'] 及其用户生成的输入值。 So i need to know how to set up a DF which is available for all screens.所以我需要知道如何设置适用于所有屏幕的 DF。 I could store the main df as a csv and make any screen read this csv file.我可以将主 df 存储为 csv 并让任何屏幕读取此 csv 文件。 But this does feel pretty bad.但这确实感觉很糟糕。 right now i just get:现在我得到:

     maindf= pd.concat([maindf, datatoappend], axis=1)

UnboundLocalError: local variable 'maindf' referenced before assignment UnboundLocalError:分配前引用的局部变量“maindf”

I found a nice approach using global as a workaround.我发现了一种使用 global 作为解决方法的好方法。 I just had to add some extra code i did change on the first screen:我只需要添加一些我在第一个屏幕上更改的额外代码:

class screen1(Screen):
    def redo(self):
        pass
    def press(self):
        global maindf
        maindf = pd.DataFrame()
        maindf['col'] = pd.Series([ 'A'for x in range(len(Folgenzähler))])
        pass

and on the second screen i did add global again.在第二个屏幕上我确实再次添加了全局。

class screen2(Screen):
    def press(self):
        values = []
        row = self.ids.Peter
        for row in row.children:
            for ch in reversed(row.children):
                if isinstance(ch, TextInput):
                    a = ch.text
                    print("Row 1 ist"+str(a))
                    values.append(str(a))
        
        hilf2 = copy.deepcopy(values)
        hilf2 = ' '.join(hilf2).split()
        hilf2 = np.array(hilf2).reshape(len(Folgenzähler), 3)

        datatoappend= pd.DataFrame(hilf2, columns=['A', 'B', 'C'])
        global maindf
        maindf= pd.concat([maindf, datatoappend], axis=1)

        print("Pandas maindf")
        print(maindf)

Maybe there are some better ways to do this.也许有一些更好的方法可以做到这一点。 Feel free to contribute alternative approaches, critics or additional Information.随意提供替代方法、批评者或其他信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM