Data Visualization in Excel Using Python
Data Visualization in Excel Using Python
towardsdatascience.com/data-visualization-in-excel-using-python-94feeacda2fc
Source: By Author
Excel is widely used for data analysis and has a lot of functionalities for analyzing,
manipulating, visualizing, etc. Using excel should be one of the main skills required for a
Data Analyst, Product Analyst, and Business Analyst. It helps in understanding the data
and how we can use it for generating useful insights.
Python is also widely used for Data Analysis purposes and also overcomes the drawbacks
of Excel. With a little knowledge of Python, we can enhance our Data Analysis skills and
also generate more useful insights.
Combining both Excel and Python can be fruitful and makes Data Analysis more
interesting. ExcelWriter is used for performing different Excel operations using Python. It
provides a wide variety of functionalities like creating an Excel, writing data into an Excel
sheet, creating visualizations in Excel, etc.
In this article, we will explore ExcelWriter and create some visualizations using it.
import pandas as pd
import numpy as np
1/3
Loading Dataset
For this article, we will the famous Diabetes dataset that can be downloaded from online
sources. Also, we will create an excel file using excel writer where we will create
visualizations.
df = pd.DataFrame(pd.read_csv("/content/test.csv"))
writer = pd.ExcelWriter('diabetes.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', startrow=2)
The code given below will create the objects and different graphs that are Bar Chart,
Scatter Plot, Line Plot, and Area Chart.
book = writer.book
sheet = writer.sheets['Sheet1']# Title
bold = book.add_format({'bold': True, 'size': 24})
sheet.write('A1', 'Diabetes', bold)
format1 = book.add_format({'font_color': '#E93423'})
sheet.conditional_format('B4:E8', {'type': 'cell', 'criteria': '<=', 'value': 0,
'format': format1})# Bar Chart
chart = book.add_chart({'type': 'column'})
chart.add_series({'values': '=Sheet1!B4:B90', 'name': '=Sheet1!B3', 'categories':
'=Sheet1!$A$4:$A$8'})
chart.add_series({'values': '=Sheet1!C4:C90', 'name': '=Sheet1!C3'})
chart.add_series({'values': '=Sheet1!D4:D90', 'name': '=Sheet1!D3'})
chart.add_series({'values': '=Sheet1!E4:E90', 'name': '=Sheet1!E3'})
sheet.insert_chart('K2', chart)# Scatter Chart
chart = book.add_chart({'type': 'scatter'})
chart.add_series({'values': '=Sheet1!F4:F90', 'name': '=Sheet1!B3', 'categories':
'=Sheet1!$A$4:$A$8'})
sheet.insert_chart('K20', chart)# Line Chart
chart = book.add_chart({'type': 'line'})
chart.add_series({'values': '=Sheet1!F4:F90', 'name': '=Sheet1!B3', 'categories':
'=Sheet1!$A$4:$A$8'})
chart.add_series({'values': '=Sheet1!G4:G90', 'name': '=Sheet1!C3'})
chart.add_series({'values': '=Sheet1!H4:H90', 'name': '=Sheet1!D3'})
chart.add_series({'values': '=Sheet1!I4:I90', 'name': '=Sheet1!E3'})
sheet.insert_chart('S2', chart)# Area Chart
chart = book.add_chart({'type': 'area'})
chart.add_series({'values': '=Sheet1!A4:F90', 'name': '=Sheet1!B3', 'categories':
'=Sheet1!$A$4:$A$8'})
chart.add_series({'values': '=Sheet1!B4:G90', 'name': '=Sheet1!C3'})
chart.add_series({'values': '=Sheet1!H4:H90', 'name': '=Sheet1!D3'})
chart.add_series({'values': '=Sheet1!I4:I90', 'name': '=Sheet1!E3'})
sheet.insert_chart('S20', chart)writer.save()
Visualizations(Source: By Author)
2/3
Here you can clearly visualize different types of charts that we created using excel writer.
We created these visualizations directly in an Excel sheet so you need to open the excel to
visualize these graphs. Try this with different datasets, create different visualizations, and
let me know your comments in the response section.
Piyush Ingale
.
Before You Go
Thanks for reading! If you want to get in touch with me, feel free to reach me at
[email protected] or my LinkedIn Profile. You can view my Githubprofile for
different data science projects and packages tutorials. Also, feel free to explore my
profile and read different articles I have written related to Data Science.
3/3