- import pandas as pd
- import numpy as np
- import matplotlib.pyplot as plt
- from datetime import datetime
- #Download the data
- import urllib.request
- urllib.request.urlretrieve("https://www.dropbox.com/s/2dqqv1984h3mb5s/Placement_Data_Full_Class.csv?dl=1", "Placement_Data_Full_Class.csv")
- data = pd.read_csv("Placement_Data_Full_Class.csv")
- #Show the first 5 top rows
- #print(data.head(5))
- #Pivoting The Data:
- #Firstly, check the rows and columns you want to include in pivot table
- pivot=pd.pivot_table(
- data,
- values="sl_no",
- index=["degree_t","specialisation"],
- columns=["status","gender"], aggfunc=np.sum
- )
- pivot=pivot.fillna(0).astype(np.int64)
- #print(pivot)
- #We can achieve the same by
- pivot=data.pivot_table(index=["degree_t","specialisation"],
- columns=["status","gender"],
- values="sl_no",aggfunc=np.sum).fillna(0)
- #print(pivot)
- #Let us make columns more readable
- new_columns = [
- 'Unplaced_Females',
- 'Placed_Females',
- 'Unplaced_Males',
- 'Placed_Males'
- ]
- pivot.columns = new_columns
- print(pivot)
- #Let us plot the data
- plot = pivot.plot(kind="bar",figsize=(10.5,6))
- plot.tick_params(rotation=40)
- plot
- plt.title("Placement Bar Chart")
- plt.tight_layout()
- plt.show()
- #Convert this report into HTML, itcan be styled by using CSS
- #Saving plot image to local file
- image = plot.get_figure().savefig('plot.png')
- image_tag = '<img src="plot.png" class="center">'
- #writing HTML Content
- heading = '<h1> Automated Report </h1>'
- subheading = '<h3> Sample Report for Placement </h3>'
- #Using .now() from datetime library to add Time stamp
- now = datetime.now()
- current_time = now.strftime("%m/%d/%Y %H:%M:%S")
- header = '<div class="top">' + heading + subheading +'</div>'
- footer = '<div class="bottom"> <h3 style="text-align: center;"> This Report has been Generated on '+ current_time +'</h3> </div>'
- content = '<div class="table"> '+pivot.to_html()+' </div> <div class="chart"> '+ image_tag +'</div>'
- #Concating everything to a single string
- html = header + content + footer
- # Adding CSS to HTML
- css = """<style> body {\n text-align:center; \n}\n table{\n margin:0px auto;\n}</style>"""
- html = html + css
- #Writing the file
- with open("report.html","w+") as file:
- file.write(html)
- #Produce pdf report
- #from weasyprint import HTML
- #HTML('report.html').write_pdf('report.pdf')
-
- Active Topics
-
-
- by Eli 11 hours ago All in One: YouTube, TED, X, Facebook and Instagram Reels, Videos, Images and Text Posts View the latest post Replies 326 Views 31935
- by Eli 1 day ago Russia Invades Ukraine View the latest post Replies 662 Views 232886
- by Eli 1 day ago PySpark for Large Data Processing View the latest post Replies 1 Views 4379
- by Eli 3 days ago Re: What is in Your Mind? View the latest post Replies 700 Views 297572
- by Eli 1 week ago President Museveni's Speech During International Development Association (IDA) Summit View the latest post Replies 1 Views 252
- by Eli 1 week ago From Simple Linear Regression Analysis to Covariance & Correlation to Independent Determinant, and R-Squared View the latest post Replies 11 Views 24726
- by Eli 1 week ago Collection of Greatest Christian Hymns of all Times View the latest post Replies 34 Views 65262
- by Eli 1 week ago Pondering Big Cosmology Questions Through Lectures and Dialogues View the latest post Replies 34 Views 57950
- by Eli 1 week ago Programmatically Manipulate Files: Renaming, Reading, Writing, Deleting, and Moving Files Between Folders View the latest post Replies 7 Views 13783
- by Eli 2 weeks ago Iran Launches Retaliatory Attack Against Israel, and Israel Retaliates by Attacking Iranian Isfahan Millitary Base View the latest post Replies 28 Views 15085
-
Automate Reports with Python and Pandas, Save the Output to HTML
- Eli
- Senior Expert Member
- Reactions: 183
- Posts: 5378
- Joined: 9 years ago
- Location: Tanzania
- Has thanked: 75 times
- Been thanked: 88 times
- Contact:
This code (adopted from here) automates some report for the mock placement data taken from Kaggle. The generated tabular report and chart are saved to HTML. Run this code at programming-and-computing-with-python-r ... forum-6300
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
-
- Similar Topics
- Replies
- Views
- Last post
-
- Information
-
Who is online
Users browsing this forum: No registered users and 3 guests