Informatics Practices
Consider following code when conn is the name of established connection to MySQL database.
sql = SELECT * from Sales where zone = "central";
df = pandas.read_sql(sql, conn)
df.head()
What will be stored in df ?
Answer
The DataFrame df includes all the columns where 'zone' equals 'central'.
Explanation
The code executes an SQL query to select all columns (*) from the "Sales" table where the "zone" column equals to "central". It then reads the results of the query into a pandas DataFrame df using pandas.read_sql(). Then, it returns the first five rows of df using df.head().
Related Questions
What is the difference between following two statements ?
(i)
df.to_sql('houses', con = conn, if_exists = 'replace')(ii)
df.to_sql('houses', con = conn, if_exists = 'replace', index = False)Consider following code when conn is the name of established connection to MySQL database.
Cars = {'Brand': ['Alto', 'Zen', 'City', 'Kia'], 'Price': [22000, 25000, 27000, 35000]} df = pd.DataFrame(Cars, columns= ['Brand', 'Price']) df.to_sql('CARS', conn, if_exists = 'replace', index = False)What will be the output of following query if executed on MySQL ?
SELECT * from CARS ;Write a program to read details such as Item, Sales made in a DataFrame and then store this data in a CSV file.
Write a program to read data from a CSV file where separator character is '@'. Make sure that :
- the top row is used as data, not as column headers.
- only 20 rows are read into DataFrame.