import pandas as pd
my_dict={
'NAME':['Ravi','Raju','Alex'],
'ID':[1,2,3],'MATH':[30,40,50],
'ENGLISH':[20,30,40]
}
df = pd.DataFrame(data=my_dict) # DataFrame from dictionary
df.to_excel('D:\my_file.xlsx') # create Excel file
df.to_excel('D:\my_file.xlsx',index=False)
df.to_excel('D:\my_file.xlsx')
Inside data directory
df.to_excel('D:\data\my_file.xlsx')
df.to_excel('D:\my_file.xlsx',na_rep='*')
df2.to_excel('C:\\data\\student2.xlsx', columns=['id','name'])
my_heads=['my_id','my_names','my_class','my_mark','my_gender']
df2.to_excel('C:\\data\\student2.xlsx',header=my_heads)
df2.to_excel('C:\\data\\student2.xlsx',startrow=3,startcol=2)
df2.to_excel('C:\\data\\student2.xlsx',freeze_panes=(1,5))
float_format="%.2f"
will update the value 0.1234 to 0.12
df.to_excel('D:\my_file.xlsx',index=False,sheet_name='my_Sheet_1')
This will store our data in my_sheet_1 worksheet of file name my_file.xlsx at root of D drive.
import pandas as pd
my_dict={
'NAME':['Ravi','Raju','Alex'],
'ID':[1,2,3],'MATH':[30,40,50],
'ENGLISH':[20,30,40]
}
df = pd.DataFrame(data=my_dict)
df2 = df.copy() # copy of df
with pd.ExcelWriter('D:my_file.xlsx') as my_excel_obj: #Object created
df.to_excel(my_excel_obj,sheet_name='my_Sheet_1')
df2.to_excel(my_excel_obj,sheet_name='my_Sheet_2')
Above code will create excel file with two worksheets. Here the new file name is my_file.xlsx with two worksheets.
import pandas as pd
my_dict={
'NAME':['Ravi','Raju','Alex'],
'ID':[1,2,3],'MATH':[30,40,50],
'ENGLISH':[20,30,40]
}
df = pd.DataFrame(data=my_dict)
df2 = df.copy() # copy of df
with pd.ExcelWriter('D:my_file.xlsx',mode='a',engine='openpyxl') as my_excel_obj:
df.to_excel(my_excel_obj,sheet_name='my_Sheet_3')
df2.to_excel(my_excel_obj,sheet_name='my_Sheet_4')
This code will add two more worksheets my_Sheet_3 and my_Sheet_4 to existing workbook my_file.xlsx .
import mysql.connector
import pandas as pd
my_connect = mysql.connector.connect(
host="localhost",
user="userid",
passwd="password",
database="my_tutorial"
)
####### end of connection ####
sql="SELECT * FROM student "
df = pd.read_sql(sql,my_connect )
df.to_excel('D:\my_file.xlsx')
In above code we have first connected to MySQL database and then collected the records of student table by using read_sql() to a DataFrame. Finally we saved them in an excel file using to_excel().
import pandas as pd
from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://userid:pw@localhost/my_db")
sql="SELECT * FROM student "
df = pd.read_sql(sql,my_conn)
df.to_excel('D:\\my_data\\student.xlsx') # Add the path
We can collect data of different classes from our student table and store them in one excel file by keeping in different worksheets. So all students of class Four will be in one worksheet named as Four and similarly another worksheet for class Three students. You can extend this to other classes also.
import pandas as pd
from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://userid:pw@localhost/my_db")
sql="SELECT * FROM student WHERE class='Three'"
sql2="SELECT * FROM student WHERE class='Four'"
df=pd.read_sql(sql,my_conn) # class Three students
df2=pd.read_sql(sql2,my_conn) # class Four students
with pd.ExcelWriter('D:\\my_data\\student.xlsx',engine='openpyxl') as my_obj:
df.to_excel(my_obj,sheet_name='Three')
df2.to_excel(my_obj,sheet_name='Four')
More about xlsxwriterimport pandas as pd
df=pd.read_csv('test.csv')
df=df.loc[:,['class','name']]
df = pd.DataFrame(data=df)
df.to_excel('my_file.xlsx',index=False)
We can use various other filters to manage the data and store in CSV file. You can rad more on filters sections.
to_excel()
function?sheet_name
parameter in the to_excel()
function?to_excel()
function?to_excel()
function?to_excel()
function to customize the Excel export?to_excel()
function?to_excel()
function?to_excel()
function?to_excel()
function support for exporting DataFrames?Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.