-
Notifications
You must be signed in to change notification settings - Fork 4
Interacting with datasets in MySQL
Sean Quigley edited this page Nov 3, 2015
·
18 revisions
Data has been loaded into a MySQL relational database for your consumption. This is particularly useful for especially large datasets that can be hard to load into memory on your local machine or are slow to download from S3.
Log in:
$ mysql -h health-db-internet.c6clocfz5zxy.us-east-1.rds.amazonaws.com -P3306 -u data_hacker -p
Enter password:
SHOW available databases:
MySQL [(none)]> show databases;
+------------------------------------------------+
| Database |
+------------------------------------------------+
| information_schema |
| CMS_open_payments_2013 |
| CMS_open_payments_2014 |
| Chronic_conditions_PUFs_2008 |
| Chronic_conditions_PUFs_2010 |
| Elhadad |
| Medicare_provider_utilization_and_payment_data |
| NPDB_malpractice_claims |
| SPARCS |
| innodb |
| mysql |
| performance_schema |
+------------------------------------------------+
12 rows in set (0.11 sec)
Describe a table:
MySQL [CMS_open_payments_2013]> use CMS_open_payments_2013;
Database changed
MySQL [CMS_open_payments_2013]> show tables;
+----------------------------------+
| Tables_in_CMS_open_payments_2013 |
+----------------------------------+
| general_payment_data |
| ownership_payment_data |
| research_payment_data |
+----------------------------------+
3 rows in set (0.32 sec)
MySQL [CMS_open_payments_2013]> desc ownership_payment_data;
+------------------------------------------------------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------------------------------------------------+--------------+------+-----+---------+-------+
| Physician_Profile_ID | bigint(20) | YES | | NULL | |
| Physician_First_Name | varchar(255) | YES | | NULL | |
| Physician_Middle_Name | varchar(255) | YES | | NULL | |
| Physician_Last_Name | varchar(255) | YES | | NULL | |
| Physician_Name_Suffix | varchar(255) | YES | | NULL | |
| Recipient_Primary_Business_Street_Address_Line1 | varchar(255) | YES | | NULL | |
| Recipient_Primary_Business_Street_Address_Line2 | varchar(255) | YES | | NULL | |
| Recipient_City | varchar(255) | YES | | NULL | |
| Recipient_State | varchar(255) | YES | | NULL | |
| Recipient_Zip_Code | varchar(255) | YES | | NULL | |
| Recipient_Country | varchar(255) | YES | | NULL | |
| Recipient_Province | varchar(255) | YES | | NULL | |
| Recipient_Postal_Code | bigint(20) | YES | | NULL | |
| Physician_Primary_Type | varchar(255) | YES | | NULL | |
| Physician_Specialty | varchar(255) | YES | | NULL | |
| Record_ID | bigint(20) | YES | | NULL | |
| Program_Year | bigint(20) | YES | | NULL | |
| Total_Amount_Invested_USDollars | double | YES | | NULL | |
| Value_of_Interest | double | YES | | NULL | |
| Terms_of_Interest | text | YES | | NULL | |
| Submitting_Applicable_Manufacturer_or_Applicable_GPO_Name | varchar(255) | YES | | NULL | |
| Applicable_Manufacturer_or_Applicable_GPO_Making_Payment_ID | bigint(20) | YES | | NULL | |
| Applicable_Manufacturer_or_Applicable_GPO_Making_Payment_Name | varchar(255) | YES | | NULL | |
| Applicable_Manufacturer_or_Applicable_GPO_Making_Payment_State | varchar(255) | YES | | NULL | |
| Applicable_Manufacturer_or_Applicable_GPO_Making_Payment_Country | varchar(255) | YES | | NULL | |
| Dispute_Status_for_Publication | varchar(255) | YES | | NULL | |
| Interest_Held_by_Physician_or_an_Immediate_Family_Member | varchar(255) | YES | | NULL | |
| Payment_Publication_Date | varchar(255) | YES | | NULL | |
+------------------------------------------------------------------+--------------+------+-----+---------+-------+
28 rows in set (0.03 sec)
Run a query:
MySQL [CMS_open_payments_2013]> select Physician_Profile_ID, Value_of_Interest FROM ownership_payment_data WHERE Value_of_Interest > 1000000 ORDER BY Value_of_Interest DESC LIMIT 5;
+----------------------+-------------------+
| Physician_Profile_ID | Value_of_Interest |
+----------------------+-------------------+
| 240414 | 158911292.87 |
| 1315076 | 127454475.84 |
| 75460 | 41580000 |
| 34470 | 41040000 |
| 1210535 | 24708809.59 |
+----------------------+-------------------+
5 rows in set (0.07 sec)
Ensure you have the MySQL Python library installed:
$ pip install MySQL-python
An example script can be found here.
Running the script:
$ python mysql_python.py
Please enter password for data_hacker:
Running query: SELECT * FROM CMS_open_payments_2013.general_payment_data WHERE Recipient_State="WY";
Columns: ['Covered_Recipient_Type', 'Teaching_Hospital_ID', 'Teaching_Hospital_Name', 'Physician_Profile_ID', 'Physician_First_Name', 'Physician_Middle_Name', 'Physician_Last_Name', 'Physician_Name_Suffix', 'Recipient_Primary_Business_Street_Address_Line1', 'Recipient_Primary_Business_Street_Address_Line2', 'Recipient_City', 'Recipient_State', 'Recipient_Zip_Code', 'Recipient_Country', 'Recipient_Province', 'Recipient_Postal_Code', 'Physician_Primary_Type', 'Physician_Specialty', 'Physician_License_State_code1', 'Physician_License_State_code2', 'Physician_License_State_code3', 'Physician_License_State_code4', 'Physician_License_State_code5', 'Submitting_Applicable_Manufacturer_or_Applicable_GPO_Name', 'Applicable_Manufacturer_or_Applicable_GPO_Making_Payment_ID', 'Applicable_Manufacturer_or_Applicable_GPO_Making_Payment_Name', 'Applicable_Manufacturer_or_Applicable_GPO_Making_Payment_State', 'Applicable_Manufacturer_or_Applicable_GPO_Making_Payment_Country', 'Total_Amount_of_Payment_USDollars', 'Date_of_Payment', 'Number_of_Payments_Included_in_Total_Amount', 'Form_of_Payment_or_Transfer_of_Value', 'Nature_of_Payment_or_Transfer_of_Value', 'City_of_Travel', 'State_of_Travel', 'Country_of_Travel', 'Physician_Ownership_Indicator', 'Third_Party_Payment_Recipient_Indicator', 'Name_of_Third_Party_Entity_Receiving_Payment_or_Transfer_of_Valu', 'Charity_Indicator', 'Third_Party_Equals_Covered_Recipient_Indicator', 'Contextual_Information', 'Delay_in_Publication_Indicator', 'Record_ID', 'Dispute_Status_for_Publication', 'Product_Indicator', 'Name_of_Associated_Covered_Drug_or_Biological1', 'Name_of_Associated_Covered_Drug_or_Biological2', 'Name_of_Associated_Covered_Drug_or_Biological3', 'Name_of_Associated_Covered_Drug_or_Biological4', 'Name_of_Associated_Covered_Drug_or_Biological5', 'NDC_of_Associated_Covered_Drug_or_Biological1', 'NDC_of_Associated_Covered_Drug_or_Biological2', 'NDC_of_Associated_Covered_Drug_or_Biological3', 'NDC_of_Associated_Covered_Drug_or_Biological4', 'NDC_of_Associated_Covered_Drug_or_Biological5', 'Name_of_Associated_Covered_Device_or_Medical_Supply1', 'Name_of_Associated_Covered_Device_or_Medical_Supply2', 'Name_of_Associated_Covered_Device_or_Medical_Supply3', 'Name_of_Associated_Covered_Device_or_Medical_Supply4', 'Name_of_Associated_Covered_Device_or_Medical_Supply5', 'Program_Year', 'Payment_Publication_Date']
Number of records: 2333
An example script can be found here.
Running the script:
$ python pandas_example.py
starting call to sql
finished processing ask
An example script can be found here.
Running the script:
$ r
...
> source("r_mysql_example.R")
Loading required package: DBI
> head(result)
Recipient_State avg(d.Total_Amount_of_Payment_USDollars)
1 AE 96.20500
2 AK 23.75760
3 AL 48.47898
4 AR 277.37596
5 AZ 131.19013
6 CA 284.60446