Informatics Practices
What additional argument do you need to specify in to_sql() so that old data of MySQL table is retained ?
Python Pandas
1 Like
Answer
The if_exists argument in the pandas to_sql() function retains old data in a MySQL table. Using if_exists = append appends new data without deleting existing data.
Answered By
2 Likes
Related Questions
What is pymysql library of Python ?
What all libraries do you require in order to interact with MySQL databases (and DataFrame) from within Python ?
If query is a string storing an SQL statement. Write statements so that the data is fetched based on query from SQL database Mydata.
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)