Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

If possible, convert the sqlite3 portion of the script below into a python script that does the exact same thing (can use any python libraries).

If possible, convert the sqlite3 portion of the script below into a python script that does the exact same thing (can use any python libraries). The Python script would be referencing a Pandas Data Frame with the same data and structure as the database the sql refers to. The original assignment question read as follows: "Add to each customer's record whether the customer has any form of each of the following credit cards: AMEX, Discover, VISA, and Mastercard. Create for each type of card whether a customer has one with a "Y" or a "N". Document your creation of these codes by showing how they are related to the code values in the data."

Here is the SQLITE3 query that should be converted into a python script.

image text in transcribed

# PART 3.b) Indicate whether customer has Visa, Mastercard, AMEX, Disc with a Y' or "N. Can be 4 columns #create a query to display the creditcard details and execute cursor cur concursor() # curser required to execute a SQL statement or query cur.execute(""" SELECT acctno, CASE WHEN ((amex_prem IS NOT NULL AND amex prem- 'Y") or (amex_reg is not null and amex_reg-'Y")) THEN 'Y ELSE 'N END AS AMEX, CASE WHEN ((visa-prem IS NOT NULL AND visa-prem='Y') or (visa_reg is not null and visa_reg-'Y")) THEN 'Y ELSE N END AS VISA, CASE WHEN ((mc_prem IS NOT NULL AND mc_prem-'Y') or (mc_reg is not null and mc_reg-'Y')) THEN Y ELSE 'N END AS MASTER CASE WHEN ((disc_prem IS NOT NULL AND disc_prem-'Y") or (disc reg is not null and disc reg='Y')) THEN Y ELSE 'N END AS DISCOVERY FROM customer" " ") #curfetchaLL() will fetche the rows from the cursor df-creditcard = pd . DataFrame(cur .fetchall()) #get the column names from cursor using cur.description and add to the datatraem field names = [ifaj for i in cur.description df creditcard . columns field names In [58]: df_creditCard ut [58] acctno AMEX VISA MASTER DISCOVERY 0 SSYSPDHQH 1WQSDDAWQS HHSSPSD 3 PHLHSYLWP 4 PSDSQYYDW # PART 3.b) Indicate whether customer has Visa, Mastercard, AMEX, Disc with a Y' or "N. Can be 4 columns #create a query to display the creditcard details and execute cursor cur concursor() # curser required to execute a SQL statement or query cur.execute(""" SELECT acctno, CASE WHEN ((amex_prem IS NOT NULL AND amex prem- 'Y") or (amex_reg is not null and amex_reg-'Y")) THEN 'Y ELSE 'N END AS AMEX, CASE WHEN ((visa-prem IS NOT NULL AND visa-prem='Y') or (visa_reg is not null and visa_reg-'Y")) THEN 'Y ELSE N END AS VISA, CASE WHEN ((mc_prem IS NOT NULL AND mc_prem-'Y') or (mc_reg is not null and mc_reg-'Y')) THEN Y ELSE 'N END AS MASTER CASE WHEN ((disc_prem IS NOT NULL AND disc_prem-'Y") or (disc reg is not null and disc reg='Y')) THEN Y ELSE 'N END AS DISCOVERY FROM customer" " ") #curfetchaLL() will fetche the rows from the cursor df-creditcard = pd . DataFrame(cur .fetchall()) #get the column names from cursor using cur.description and add to the datatraem field names = [ifaj for i in cur.description df creditcard . columns field names In [58]: df_creditCard ut [58] acctno AMEX VISA MASTER DISCOVERY 0 SSYSPDHQH 1WQSDDAWQS HHSSPSD 3 PHLHSYLWP 4 PSDSQYYDW

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Server Query Performance Tuning

Authors: Sajal Dam, Grant Fritchey

4th Edition

1430267429, 9781430267423

More Books

Students also viewed these Databases questions

Question

Tell the merits and demerits of Mendeleev's periodic table.

Answered: 1 week ago