Data transfer between files , SQL Database and Dataframes Type B Sumita arora IP Solution




Q1. Predict the output of following code fragments one by one. For every next code fragment, consider that the changes by previous code fragment are in place. That is, for code fragment (b), changes made by code fragment (a) are persisting; for (c), changes by (a) and (b) are persisting and so on.
(a)
import pandas as pd
columns=['2015', '2016', '2017', '2018']
index=['Messi', 'Ronaldo', 'Neymar', 'Hazard']
df = pd.DataFrame (columns = columns, index = index)
print (df)
df.to_csv("c:\one.csv")
(b)
df[ '2015' ][ 'Messi'] = 12
df['2016']['Ronaldo'] = 11
df[ '2017']['Neymar'] = 8
df['2018']['Hazard'] = 16
print (df)
df.to_csv("c:\two.csv", sep= '@')
(c)
new_df = pd.read_csv('c:\one.csv',index_col = 0)
print(new_df)
(d)
new_df = pd.read_csv('c:\one.csv')
print(new_df)
(e)
new_df = pd.read_csv('c:\two.csv')
print(new_df)
(f)
new_df = pd.read_csv('c:\two.csv', sep= '@')
print(new_df)




Q2. Are the following two statements same? Why/Why not?
(i)
pd.read_csv('zoo.csv', sep = ',')
(ii)
pd.read_csv('zoo.csv')




Q3. How are following two codes similar of different? What output will they produce?
(i)
df = pd.read_csv("data.csv", nrows = 5)
print(df)
(ii)
df = pd.read_csv("data.csv")
print(df)




Q4. 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)




Q5. 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 = 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;




Q6. 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?

2 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post