Informatics Practices
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)
Python Pandas
1 Like
Answer
The difference between the two statements is whether the DataFrame's index is included as a separate column in the resulting SQL table. By default, when we use to_sql() without specifying the index parameter, index = True is assumed, meaning that the DataFrame's index will be included in the SQL table, as in the first statement. Setting index = False explicitly excludes the DataFrame's index from being saved as a separate column in the SQL table, as in the second statement.
Answered By
1 Like
Related Questions
How are following two codes similar or 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)Write Python statement to export the DataFrame to a CSV file named data.csv stored at D: drive.
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 ;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 ?