Big data Analysis based on python-pandas data Storage (Code practice)
In the last part, we learned how to read data from pandas. This time, let's take a look at how to store the data, and the code will be rolled up.
Csv file
Format: to_csv (file path, sep='', index=TRUE, header=TRUE)
Index defaults to true with a line serial number
Header defaults to true with column name
From pandas import DataFrame
From pandas import Series
# create data
Df=DataFrame ({'age':Series ([265.85]),' name':Series (['xiaoqiang1','xiaoqiang2'])})
Df
# deposit in
Df.to_csv ('d:\ 1.csv')
Excel file
Format: to_excel (file path, index=TRUE, header=TRUE)
Explain the same as above, not nonsense
From pandas import DataFrame
From pandas import Series
# create data
Df=DataFrame ({'age':Series ([265.85]),' name':Series (['xiaoqiang1','xiaoqiang2'])})
Df
# deposit in
Df.to_excel ('d:\ 1.xlsx')
Mysql
Format: to_sql (name= table name, con= database link object)
From pandas import DataFrame
From pandas import Series
From sqlalchemy import create_engine
Engine=create_engine ('mysql+pymysql:// fill in user name: fill in password @ fill in ip address: 3306 / fill in database name? charset=utf8')
# create data
Df=DataFrame ({'age':Series ([265.85]),' name':Series (['xiaoqiang1','xiaoqiang2'])})
Df.to_sql (name= table name, con=engine, if_exists='append', index=False, index_label=False)