Include 0 to a annotate count if not found in django Queryset.? - django

I don't know how to phrase my question correctly but what I am doing is I am querying a table based on certain id and then getting the count of the unique field in a column and that count I am giving to matplotlib to plot but what problem I am facing is if a certain value is not found the label size mismatches with the count and plot doesn't work. Below is my query:
workshop_feedback = WorkshopFeedbackResponse.objects.filter(workshop_conducted=workshop_id)
count = workshop_feedback.values('q1').annotate(count=Count('q1'))
sizes = [item['count'] for item in count] #converting to list for matplotlib bar char
labels = [1,2,3] #option of choices for q1
fig, ax = plt.subplots()
ax.pie(sizes, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)
ax.axis('equal')
# Equal aspect ratio ensures that pie is drawn as a circle.
plt.savefig(os.path.join(settings.BASE_DIR, filename)) #filename is random image filename Which i am creating image of the graph
My column name is q1 and it can contain 1,2,3 as an option depending upon what user has filled but suppose if I don't have 1 or either option the count doesn't reflect that.
Exception Value:
'label' must be of length 'x'
Above is the error I get from if count of certain value is missing. So How to get count as 0 if certain label value is missing?
Edit: If I can query based on label value to get count in a column I can fix my error.

Related

Get Range with If Condition

Using app script, I'd like to create a macro that fetches a cell array from a different sheet. None of the the cells in the array actually have values though, but they do have data validation applied with specific lists for each cell.
Based on the user input, I have an IF formula in the cells above that populate a 1 or 0 if that cell needs to be included in the range to be fetched.
ROW 1 = IF formula that populates in line with user input
ROW 2 = target range
ROW 3 = formulas that populates values for each data validation list (not relevant)
enter image description here

I want to annotate my data based on (refined) named entity that I extracted by spaCy

I have worked on around 7000 sentences with Spacy in order to extract Named Entity. I extracted :
Long, Date, Star, Para, Astr, Plan, Names AS entity. As you see here in the link:
https://ibb.co/rtQm5B9
Now I want to (assign) annotate each sentence to
"observation" or "not observation" label 1 and 0
based on the Named entity that I provided. I have gathered my data in the panda data frame and I add the entity tuples
(text, type entity) like this (2h 14m, Date). Sometimes there is more than one entity in one sentences
The problem is I don't know how can I have access to tuples in each row and how can I define a new column based on the entity in each column.
My data look like this: (python dataframe)
https://ibb.co/vdfXJFD
how can I make new column based on the entity column, for instance, if there is the entity (second element in each tuple in the column enitites) with label data and long, then we can assign to new column label 1 and if not we can assign label 0
I solved it somehow:
def annot_observation_01(x):
y=0
if x is not None:
for idx in range(0,len(x)):
if any ([x[idx][1] == 'LONG', x[idx][1] == 'DATE']):
y=1
else:
y=0
else:
y=0
return y
then I add it to my dataframe like this:
dfAstroNova['label'] = dfAstroNova['entities'].apply(lambda x: annot_observation_01(x))
As you see in the code, it says if the label in entities is "LONG" or "DATE" it gives value 1 TO column label, how can I write my function if it gives value 1 when both of labels "LONG" and "DATE" appears in sentences?

replace pandas dataframe with a unique id

I got a dataframe with millions of entries, with one of the columns 'TYPE' (string). There is a total of 400 values for this specific column and I want to replace the values with integer id starting from 1 to 400. I also want to export this dictionary 'TYPE' => id for future reference. I tried with to_dict but it did not help. Anyway can do this ?
Option 1: you can use pd.factorize:
df['new'] = pd.factorize(df['str_col'])[0]+1
Option 2: using category dtype:
df['new'] = df['str_col'].astype('category').cat.codes+1
or even better just convert it to categorical dtype:
df['str_col'] = df['str_col'].astype('category')
and when you need to use numbers instead just use category codes:
df['str_col'].cat.codes
thanks to #jezrael for extending the answer - for creating a dictionary:
cats = df['str_col'].cat.categories
d = dict(zip(cats, range(1, len(cats) + 1)))
PS category dtype is very memory-efficient too

How do I change the individual value to be summed during annotation in Django?

I am front end developer new to django. There is a certain column(server_reach) in our postgres DB which has values of (1,2). But I need to write a query which tells me if at least one of the filtered rows has a row with reachable values( 1= not reachable, 2 = reachable).
I was initially told that the values of the column would be (0,1) based on which I wrote this:
ServerAgent.objects.values('server').filter(
app_uuid_url=app.uuid_url,
trash=False
).annotate(serverreach=Sum('server_reach'))
The logic is simple that I fetch all the filtered rows and annotate them with the sum of the server_reaches. If this is more than zero then at least one entry is non-zero.
But the issue is that the actual DB has values (1,2). And this logic will not work anymore. I want to subtract the server_reach of each row by '1' before summing. I have tried F expressions as below
ServerAgent.objects.values('server').filter(
app_uuid_url=app.uuid_url,
trash=False
).annotate(serverreach=Sum(F('server_reach')-1))
But it throws the following error. Please help me getting this to work.
AttributeError: 'ExpressionNode' object has no attribute 'split'
Use Avg instead of Sum. If average value is greater than 1 then at least one row contains value of 2.

Doctrine distinct createQueryBuilder

I am trying to select MAX scores objects for unique user(s).
So user should be distinct and i should receive max score for a user (order), and I need to get other object values like dataAdded for this max score.
$query = $this->createQueryBuilder('s');
$query->select('DISTINCT s.user, s');
$query->where('s.challenge = :challenge')->setParameter('challenge', $challenge);
$query->andWhere('s.date_added BETWEEN :monday AND :sunday')
->setParameter('monday', $startDate->format('Y-m-d'))
->setParameter('sunday', $endDate->format('Y-m-d'));
$query->orderBy('s.score', 'DESC');
//$query->groupBy('s.user');
$query->setMaxResults($limit);
$results = $query->getQuery();
Does not matter how I try i can't make distinct to work. I have tried group by MAX(s.score) but this will associate random object with max score rather then the same object..
Does anyone have a solution to this? Distinct simply don't seem to work...
Generated query:
SELECT DISTINCT s.user, s FROM Digital\ApplicationBundle\Entity\ChallengeScore s WHERE s.challenge = :challenge AND (s.date_added BETWEEN :monday AND :sunday) ORDER BY s.score DESC
But an error ;(
[Semantical Error] line 0, col 18 near 'user, s FROM': Error: Invalid PathExpression. Must be a StateFieldPathExpression
Its worth to mention that user is a relation. When i try this on other files this works fine...
If you need get count distinct values, you can use countDistinct. But if you must get all distinct values, use groupBy