从Django表单到pandas DataFrame

时间:2021-12-03 22:58:09

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?

我对Django很新,但已经面临相当艰巨的任务。我需要在用户提供输入的网页上创建多个这样的表单(只允许浮动数字),然后将这些输入转换为pandas DataFrame进行数据分析。如果你能告诉我该如何做这件事我会非常感激?

Form needed:

表格需要:

从Django表单到pandas DataFrame

1 个解决方案

#1


0  

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

这是一个非常广泛的问题,我假设你熟悉pandas和python。可能有一种更有效的方法,但这就是我要做的。用户提交表单然后在视图中导入pandas应该不会那么困难。创建初始数据框然后您可以使用类似的东西获取表单数据

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)

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.

以这种方式继续迭代数据,直到添加完所有数据。在全部附加之后,您可以进行分析以及您喜欢的任何其他内容。你注意到你可以附加更多的数据2比2,例如。

Pandas append docs:

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

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

Django forms docs:

https://docs.djangoproject.com/en/2.0/topics/forms/

https://docs.djangoproject.com/en/2.0/topics/forms/

The docs are you friend

文档是你的朋友

#1


0  

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

这是一个非常广泛的问题,我假设你熟悉pandas和python。可能有一种更有效的方法,但这就是我要做的。用户提交表单然后在视图中导入pandas应该不会那么困难。创建初始数据框然后您可以使用类似的东西获取表单数据

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)

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.

以这种方式继续迭代数据,直到添加完所有数据。在全部附加之后,您可以进行分析以及您喜欢的任何其他内容。你注意到你可以附加更多的数据2比2,例如。

Pandas append docs:

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

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

Django forms docs:

https://docs.djangoproject.com/en/2.0/topics/forms/

https://docs.djangoproject.com/en/2.0/topics/forms/

The docs are you friend

文档是你的朋友