从Django表单到Pandas DataFrame [英] from Django forms to pandas DataFrame

查看:249
本文介绍了从Django表单到Pandas DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Django非常陌生,但是已经面临着艰巨的任务. 我需要在网页上创建多个这样的表单,用户可以在其中提供输入(仅允许使用浮点数),然后将这些输入转换为pandas DataFrame进行数据分析.如果您能提出建议,我将不胜感激.

I am very new to Django, but facing quite a daunting task already. I need to create multiple forms like this on the webpage where user would provide input (only floating numbers allowed) and then convert these inputs to pandas DataFrame to do data analysis. I would highly appreciate if you could advise how should I go about doing this?

所需表格:

推荐答案

这是一个非常广泛的问题,我假设您熟悉熊猫和python.也许有一种更有效的方法,但这就是我要做的.用户提交表单然后在您看来导入熊猫并不难.创建一个初始数据框然后您可以使用类似这样的方式获取表单数据

This is a very broad question and I am assuming you are familiar with pandas and python. There might be a more efficient way but this is how I would do it. It should not be that difficult have the user submit the form then import pandas in your view. Create an initial data frame Then you can get the form data using something like this

if form.is_valid():
    field1 = form.cleaned_data['field1']
    field2 = form.cleaned_data['field2']
    field3 = form.cleaned_data['field3']
    field4 = form.cleaned_data['field4']

然后您可以像这样创建一个新的数据框:

you can then create a new data frame like so:

df2 = pd.DataFrame([[field1, field2], [field3, field4]], columns=list('AB'))

然后将第二个数据帧附加到第一个数据帧,如下所示:

then append the second data frame to the first like so:

df.append(df2)

以这种方式遍历数据,直到添加完所有数据为止.将其全部添加后,您可以进行分析以及您喜欢的其他任何事情.您注意到您可以附加更多的数据,例如2×2.

Keep iterating over the data in this fashion until you have added all the data. After its all been appended you can do you analysis and whatever else you like. You note you can append more data the 2 by 2 thats just for example.

https://pandas.pydata.org /pandas-docs/stable/genic/pandas.DataFrame.append.html

https://docs.djangoproject.com/en/2.0/topics/表格/

文档是您的朋友

这篇关于从Django表单到Pandas DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆