from Django forms to pandas DataFrame

三世轮回 提交于 2019-12-23 04:58:11

问题


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?

Form needed:


回答1:


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)

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.

Pandas append docs:

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

Django forms docs:

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

The docs are you friend



来源:https://stackoverflow.com/questions/50728644/from-django-forms-to-pandas-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!