1. #need to use pip to get requests, pandas, and
  2. import requests # Import the requests library to make HTTP requests
  3. import pandas as pd # Import the pandas library for data manipulation and analysis
  4. def get_adverse_events(drug_name, skip=0):
  5. # Define the URL for the FDA API request
  6. url = f"https://api.fda.gov/drug/event.json?search=patient.drug.medicinalproduct:{drug_name}+AND+receivedate:[20220101+TO+20220105]&limit=100&skip={skip}"
  7. response = requests.get(url) # Make a GET request to the FDA API
  8. if response.status_code == 200: # Check if the request was successful (status code 200)
  9. return response.json() # Return the response data as a JSON object
  10. else:
  11. return None # Return None if the request was not successful
  12. statins = ["atorvastatin", "rosuvastatin", "simvastatin", "pravastatin", "lovastatin"]
  13. # List of statin drugs to search for adverse events
  14. all_data = [] # Initialize an empty list to store all adverse event data
  15. for statin in statins: # Loop through each statin in the list
  16. skip = 0 # Initialize the skip parameter to 0 for pagination
  17. while True: # Infinite loop to keep fetching data until no more results
  18. data = get_adverse_events(statin, skip) # Get adverse events data for the current statin
  19. if not data or 'results' not in data: # Check if there is no data or no results
  20. break # Exit the loop if no more data
  21. for event in data['results']: # Loop through each event in the results
  22. adverse_event = {
  23. 'Drug': statin.capitalize(), # Capitalize the drug name
  24. 'Reaction': event['patient']['reaction'][0]['reactionmeddrapt'] if 'reaction' in event['patient'] else None,
  25. # Get the reaction type if available
  26. 'Outcome': event['patient']['reaction'][0].get('reactionoutcome', None),
  27. # Get the reaction outcome if available
  28. 'Date': event['receivedate'], # Include the receivedate
  29. 'Age': event['patient'].get('patientonsetage', None),
  30. # Get the patient's age if available
  31. 'Age Unit': event['patient'].get('patientonsetageunit', None),
  32. # Get the age unit (e.g., years, months) if available
  33. 'Gender': event['patient'].get('patientsex', None), # Get the patient's gender if available
  34. }
  35. all_data.append(adverse_event) # Add the adverse event data to the list
  36. skip += 100 # Increment the skip parameter to get the next set of results (we already got these, so it's skipping what we got and is going to the next batch)
  37. df = pd.DataFrame(all_data) # Create a DataFrame from the list of adverse event data
  38. # Save the DataFrame to an Excel file
  39. df.to_excel('adverse_events_statins.xlsx', index=False)