Reputation: 190
I am using django-restframework,pandas,django-pandas to make an api and i am getting the following output
This is the data of a single user and each label represents a column name but I want the output in the following format
can anyone help to get the data in the desired format
My code is
views.py
@api_view(['GET'])
def my_view(request,id):
qs = Health.objects.filter(id = id)
df = read_frame(qs)
df['x-Mean'] = abs(df['Age'] - df['Age'].mean())
df['1.96*std'] = 1.96*df['Age'].std()
df['Outlier'] = abs(df['Age'] - df['Age'].mean()) > 1.96*df['Age'].std()
df['bmi'] = df['Weight']/(df['Height']/100)**2
a = df.fillna(0)
return Response(a)
Upvotes: 1
Views: 1288
Reputation: 2249
This is happening because a
is a pandas.DataFrame
and it corresponds to table, so during serialization it tries to represent all data for each table column. DataFrame
does not know that you have only one value for each column.
Values have to be extracted manually:
a = {column: values[0] for column, values in df.fillna(0).to_dict().items(orient='list')}
return Response(a)
For more details check http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_dict.html
Upvotes: 3