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

Publicado em: 07 Maio 2021
no canal de: plus2net
1,371
14

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.
https://www.plus2net.com/python/panda...

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
my_data.to_csv('my_file.csv')

This will create the my_file.csv file in the same directory. A different path can be given to store the file in different location in the system.
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
my_conn = create_engine("mysql+mysqldb://root:test@localhost/my_tutorial")
sql="SELECT * FROM student LIMIT 0,10"
my_data=pd.read_sql(sql,my_conn)
my_data.to_csv('my_file.csv')

Here 10 rows of data from the student table is taken and the csv file is created.
#pandas_to_csv #dataframetocsv #pandascsv


Nesta página do site você pode assistir ao vídeo on-line Python Pandas DataFrame to creating csv file and using MySQL sample table to csv by using to_csv() duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário plus2net 07 Maio 2021, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 1,371 vezes e gostou 14 espectadores. Boa visualização!