We will save data to CSV ( comma separated values ) file. This is a format used for storing data which can easily exported to MS Excel or any other database.
We can remove the column names and store only the data part. By default it is True and all column headers are used.
my_data.to_csv('my_file.csv',header=False)
Storing part of the data
We can filter the DataFrame and then save the rows in CSV file. For this we will use our test.csv file as we have more rows.
Now let us store only two columns, class and name
import pandas as pd
my_data=pd.read_csv('test.csv')
df=my_data.loc[:,['class','name']]
my_data = pd.DataFrame(data=df)
my_data.to_csv('my_file.csv',index=False)
We can use various other filters to manage the data and store in CSV file. You can rad more on filters sections.
Data from MySQL table to CSV file
Read on how to connect to MySQL database and then collected the records of student table by using read_sql() to a DataFrame. Finally we will save them in an CSV file using to_csv().
import mysql.connector
import pandas as pd
my_connect = mysql.connector.connect(
host="localhost",
user="root",
passwd="test",
database="my_tutorial"
)
####### end of connection ####
sql="SELECT * FROM student "
my_data = pd.read_sql(sql,my_connect )
my_data.to_csv('D:\my_file.csv')
Using SQLAlchemy
Read more on MySQL with SQLAlchemy connection. Below code will create student.csv file in the same directory, you can add path ( as above ) if you want the file to be created at different location.
import pandas as pd
from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://userid:pw@localhost/my_db")
sql="SELECT * FROM student "
my_data = pd.read_sql(sql,my_conn)
my_data.to_csv('student.csv') # Add the path