The empty result of a program written in python
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 answers
-
yg_be Posted messages 23437 Registration date Status Contributor 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']) -
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.
-
-
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'])
-
-
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:
-
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." -
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 ] }
- Example:
- 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
-
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.
-
- The statement is incomplete: what attributes do you want to retrieve given one (or more) student identifiers? Their name?