Python Pandas DataFrame to creating csv file and using MySQL sample table to csv by using to_csv()

preview_player
Показать описание
We can create a csv ( Comma Separated Value ) file by using data from Pandas DataFrame. We will first create one Pandas DataFrame by using some sample data.

import pandas as pd
my_dict={'NAME':['Alex','Ron','Ravi'],
'ID':[1,2,3],
'MATH':[40,35,48]}
my_data=pd.DataFrame(data=my_dict)

After creating the DataFrame we will use to_csv() to create a CSV file

We can add different options while creating the CSV file and some important options are header, index and columns.
By setting index=False we can remove the index column and by setting header=False we can remove the header column. Similarly we can specify the columns to be used to crate CSV file, by default all columns are used.

From MySQL database using read_sql()
We can connect to MySQL database by using SQLAlchemy engine and after connection we will create our DataFrame by using data from sample table by using read_sql.
from sqlalchemy import create_engine
sql="SELECT * FROM student LIMIT 0,10"

Here 10 rows of data from the student table is taken and the csv file is created.
#pandas_to_csv #dataframetocsv #pandascsv
Рекомендации по теме
welcome to shbcf.ru