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")
my_conn=my_conn.connect()
sql="SELECT * FROM student "
my_data = pd.read_sql(sql,my_conn)
my_data.to_csv('student.csv') # Add the path
From SQLite database to csv file.
import pandas as pd
from sqlalchemy import create_engine
my_conn = create_engine("sqlite:///E:\\my_db\\my_db.db") # Update path
my_conn=my_conn.connect()
sql="SELECT * FROM student "
my_data = pd.read_sql(sql,my_conn)
my_data.to_csv('E:\\my_db\\sqstudent.csv') # Add the path
Questions
How do I use the to_csv() function in Pandas to save a DataFrame to a CSV file?
What are the common parameters of the to_csv() function in Pandas?
Can I specify a custom delimiter while using to_csv() in Pandas?
How can I exclude the index column while saving a DataFrame to CSV using to_csv()?
Is it possible to save only specific columns of a DataFrame using to_csv()?
What is the purpose of the header parameter in the to_csv() function?
How can I handle missing values while using to_csv() in Pandas?
Can I save a DataFrame to a CSV file without quoting the values using to_csv()?
How do I save a DataFrame to a CSV file with a different encoding using to_csv()?
Is it possible to save a DataFrame to a CSV file with a specific date format in the filename using to_csv()?