Data Visualization In Excel Using Python | by Himanshu Sharma | Towards Data Science

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.
Let’s get started...
Excelwriter is defined in Pandas so we don’t need to install it separately.
In this step, we will import all the libraries that are required for reading Excel and creating visualizations in Excel using Python.
import pandas as pdimport numpy as np
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)
In this step, we will create different types of visualization, write them to the excel file we created and then save the visualization. Before creating the visualizations we will also create objects for the writer to use for further manipulation.
The code given below will create the objects and different graphs that are Bar Chart, Scatter Plot, Line Plot, and Area Chart.
book = writer.booksheet = writer.sheets['Sheet1']# Titlebold = 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 Chartchart = 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 Chartchart = 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 Chartchart = 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 Chartchart = 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()
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.
This article is in collaboration with Piyush Ingale.
Thanks for reading! If you want to get in touch with me, feel free to reach me at hmix13@gmail.com or my LinkedIn Profile. You can view my Github profile 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.