🔥💯 Pandas || MySQL: An Effortless and Simple Approach! (With Comments and Explanation) 🔥💯
Pandas Approach\n- Use the str.contains() method to find patients with Type I Diabetes:\n\npatients_with_diabetes = patients[patients[\'conditions\'].str.contai
# Pandas Approach\n- Use the str.contains() method to find patients with Type I Diabetes:\n```\npatients_with_diabetes = patients[patients[\'conditions\'].str.contains(r\'\\bDIAB1\')]\n\n```\nThe str.contains() method with the regex pattern r\'\\bDIAB1\' checks each entry in the \'conditions\' column for the presence of \'DIAB1\'. The \\b in the pattern is a word boundary assertion that ensures \'DIAB1\' is a separate word and not part of another word. This ensures that we only get patients with Type I Diabetes and not other conditions that might contain \'DIAB1\' as part of the word.\n\n- Select only the required columns in the result DataFrame:\n```\n result_df = patients_with_diabetes[[\'patient_id\', \'patient_name\', \'conditions\']]\n```\nThe result_df DataFrame contains only the \'patient_id\', \'patient_name\', and \'conditions\' columns for patients with Type I Diabetes.\n\n# Pandas Code\n```\nimport pandas as pd\n\ndef find_patients(patients: pd.DataFrame) -> pd.DataFrame:\n    # Use the str.contains() method to find patients with Type I Diabetes\n    patients_with_diabetes = patients[patients[\'conditions\'].str.contains(r\'\\bDIAB1\')]\n    \n    # Select only the required columns\n    result_df = patients_with_diabetes[[\'patient_id\', \'patient_name\', \'conditions\']]\n    \n    return result_df\n```\n# MySQL Query\n```\nSELECT patient_id, patient_name, conditions\nFROM Patients\nWHERE conditions LIKE \'DIAB1%\' OR conditions LIKE \'% DIAB1%\';\n```\n# Explanation\n- `SELECT patient_id, patient_name, conditions:` This part of the query selects the columns \'patient_id\', \'patient_name\', and \'conditions\' from the \'Patients\' table.\n\n- `FROM Patients:` This specifies the table from which we are selecting the data, which is the \'Patients\' table in this case.\n\n- `WHERE conditions LIKE \'DIAB1%\' OR conditions LIKE \'% DIAB1%\':` This is the condition for filtering the rows. The query retrieves rows from the \'Patients\' table that meet either of the two conditions:\n\n1. `conditions LIKE \'DIAB1%\':` This condition matches rows where the \'conditions\' column starts with the string \'DIAB1\'. The % wildcard is used to match any sequence of characters after \'DIAB1\'. So, this part of the condition will match rows where \'conditions\' is exactly \'DIAB1\' or starts with \'DIAB1\' followed by any characters.\n\n2. `conditions LIKE \'% DIAB1%\':` This condition matches rows where the \'conditions\' column contains the string \' DIAB1\'. The % wildcard at the beginning allows for any characters before \' DIAB1\', and the % wildcard at the end allows for any characters after \' DIAB1\'. This part of the condition will match rows where \'conditions\' contains \' DIAB1\' as a separate word, with spaces before and after it.\n\nBy using the OR operator between the two conditions, the query retrieves rows that meet either of these conditions. It will return rows where the \'conditions\' column starts with \'DIAB1\' or contains \' DIAB1\' as a separate word.\n\nIn summary, the SQL query selects the \'patient_id\', \'patient_name\', and \'conditions\' columns from the \'Patients\' table for patients who have Type I Diabetes. Type I Diabetes is identified based on the condition that the \'conditions\' column either starts with \'DIAB1\' or contains \' DIAB1\' as a separate word.\n![upvote img.jpg](https://assets.leetcode.com/users/images/6de40c20-572d-4041-8d61-564eca15fcf3_1690992168.1768682.jpeg)\n
['MySQL', 'Pandas']