The empty result of a program written in python

mus -  
mamiemando Posted messages 33541 Registration date   Status Modérateur Last intervention   -

Hello,

I need your support on the following programming part:

""https://examen-api.s3.eu-west-1.amazonaws.com/Student/{ID}" where {ID} corresponds to a learner's identifier.

  • First, develop a set of instructions to handle a single learner, then generalize your approach in a loop that iterates through a list of identifiers, and finally define the function.
  • The simplest way to proceed is to instantiate a DataFrame from a list of dictionaries

I developed this but the result displays nothing:

import pandas as pd import requests import numpy as np import json # Send a GET request to retrieve the list of student IDs from the endpoint student_list = [] for ID in student_list:     response = requests.get(f"https://examen-api.s3.eu-west-1.amazonaws.com/Student/{ID}")     student_list = response.json()       df = pd.DataFrame(student_list)     resultat = df.groupby('Student')['ID'].nunique()     print(resultat)

5 réponses

yg_be Posted messages 23437 Registration date   Status Contributeur Last intervention   Ambassadeur 1 588
 

hello,

suggestion:

import pandas as pd import requests import numpy as np import json # Send a GET request to retrieve the list of student IDs from the endpoint student_list = [1] for ID in student_list: response = requests.get(f"https://examen-api.s3.eu-west-1.amazonaws.com/Student/{ID}") student_info = response.json() print(student_info['StudentName']) 
0
mus
 

Thank you for your response.

However, it shows me only one student with 1.

To display all the students, please? Do we need to increment the list?

Thanks.

0
blux Posted messages 2016 Registration date   Status Modérateur Last intervention   3 452
 

Hello,

Not providing a student's ID should return a list object containing all the students (which is actually what you declared in your code).

0
mus > blux Posted messages 2016 Registration date   Status Modérateur Last intervention  
 

Thank you for your feedback,

do you mean with 'student'?

result = df.groupby('Student').nunique()

print(result)

0
yg_be Posted messages 23437 Registration date   Status Contributeur Last intervention   1 588
 

Have you read the entire statement?

import pandas as pd import requests import numpy as np import json # Send a GET request to retrieve the list of student IDs from the endpoint response = requests.get(f"https://examen-api.s3.eu-west-1.amazonaws.com/Students") student_list = response.json()["StudentList"] for ID in student_list: response = requests.get(f"https://examen-api.s3.eu-west-1.amazonaws.com/Student/{ID}") student_info = response.json() print(ID,student_info['StudentName']) 
0
mus
 

What is requested is to manage a unique one, that’s why I tried result = df.groupby('Student')['ID'].nunique(), which returns nothing:

The endpoint to use is ""https://examen-api.s3.eu-west-1.amazonaws.com/Student/{ID}"" where {ID} corresponds to the identifier of a student.

Tips:

  • Do not define the function right away. First, develop a set of instructions to handle a single student, then generalize your approach in a loop that iterates through a list of identifiers, and finally define the function.
  • The simplest way to proceed is to instantiate a DataFrame from a list of dictionaries in the following form:
0
blux Posted messages 2016 Registration date   Status Modérateur Last intervention   3 452
 

Already:

student_list = [] for ID in student_list:

For me, as long as student_list is not filled, you can do all the loops you want, it will finish quickly!

I would start with an HTTP GET to get the list filled.


See you later blux "Fools dare everything.
That's how we recognize them."

0
mamiemando Posted messages 33541 Registration date   Status Modérateur Last intervention   7 935
 

Hello,

There are several things that are not right:

  • The statement is incomplete: what attributes do you want to retrieve given one (or more) student identifiers? Their name?
    • This would likely clarify why you are using groupby and nunique.
  • Your list student_list (which I would rather call student_ids) is not populated. At the same time, I would rename ID to student_id, that would be clearer. Indeed, in Python, variable names in capital letters are reserved for global "constants."
  • The attribute name containing the identifier in your DataFrame is not ID, but StudentID. To realize this, you just need to look at what returns https://examen-api.s3.eu-west-1.amazonaws.com/Student/1
    • Example:
      { "StudentID": 1, "StudentName": "David Williams", "StudentCursus": "DA", "StudentCourses": [ 4, 2, 1, 3 ] }
  • While developing your program, you should add a print statement after each instruction to see its result. This would allow you to see the state of each variable and better understand what is happening.
  • Any modules you explicitly use. In your case, you import numpy and json but you do not use them. To avoid this kind of error, you can use flake8:
  • This is not fundamental, but at the beginning of the file, there is a missing shebang. It would be nice to add it to make your script executable.

Assuming we want to retrieve the names of the students:

#!/usr/bin/env python3 import pandas as pd import requests student_ids = range(3) for student_id in student_ids: url = f"https://examen-api.s3.eu-west-1.amazonaws.com/Student/{student_id}" response = requests.get(url) student_ids = response.json() df = pd.DataFrame(student_ids) print(df) resultat = df["StudentName"][0] print(resultat)

Good luck

0
yg_be Posted messages 23437 Registration date   Status Contributeur Last intervention   1 588
 

As written on April 25, 2024, at 12:15, you can retrieve the list of student IDs like this:

response = requests.get(f"https://examen-api.s3.eu-west-1.amazonaws.com/Students") student_ids = response.json()["StudentList"] for student_id in student_ids:

I don't really see the benefit of reusing the variable "student_ids" inside the loop. At best, it's a source of confusion.

0
mus
 

Hello,

Thank you for your feedback and clarification.

Best regards

0
mamiemando Posted messages 33541 Registration date   Status Modérateur Last intervention   7 935 > mus
 

@yg_be StatutContributeur ok to retrieve the entire list at once, that's indeed better (I missed that message).

@mus Do you have all your answers? If so, can we mark the topic as resolved?

0