Spaces:
Running
Running
| from math import e | |
| import panel as pn | |
| import pandas as pd | |
| import hvplot.pandas | |
| # A class to process the dataframe and create visualizations according to the shape, nature, and distribution of the data. | |
| class Visualizations: | |
| def __init__(self, df, pn: pn): | |
| self.df = df | |
| self.pn = pn | |
| def high_level_visualization(self): | |
| # Create a high-level visualization of the dataframe | |
| # Check the number of rows and columns in the dataframe | |
| rows, columns = self.df.shape | |
| if columns > 7: | |
| widget = self.pn.Column( | |
| self.pn.pane.Markdown( | |
| """ | |
| # Data Preview | |
| ### Here is a preview of the dataset. | |
| """ | |
| ), | |
| self.pn.Column( | |
| self.pn.Column( | |
| self.pn.pane.Markdown("## Head"), | |
| self.pn.pane.DataFrame(self.df.head(5)), | |
| ), | |
| self.pn.Column( | |
| self.pn.pane.Markdown("## Describe"), | |
| self.pn.pane.DataFrame(self.df.describe(),) | |
| ), | |
| ), | |
| ) | |
| return widget | |
| else: | |
| widget = self.pn.Column( | |
| self.pn.pane.Markdown( | |
| """ | |
| # Data Preview | |
| ### Here is a preview of the dataset. | |
| """ | |
| ), | |
| self.pn.Row( | |
| self.pn.Column( | |
| self.pn.pane.Markdown("## Head"), | |
| self.pn.pane.DataFrame(self.df.head(5)), | |
| ), | |
| self.pn.Column( | |
| self.pn.pane.Markdown("## Describe"), | |
| self.pn.pane.DataFrame(self.df.describe(),) | |
| ), | |
| ), | |
| ) | |
| return widget | |
| def data_shape_visualization(self): | |
| # Create a visualization of the shape of the dataframe | |
| widget = self.pn.Column( | |
| self.pn.pane.Markdown( | |
| """ | |
| # Data Shape | |
| ### Here is the shape of the dataset. | |
| """ | |
| ), | |
| self.pn.Column( | |
| self.pn.Column( | |
| self.pn.pane.Markdown("## Shape"), | |
| self.pn.pane.DataFrame(pd.DataFrame({'Rows': [self.df.shape[0]], 'Columns': [self.df.shape[1]]}), | |
| )), | |
| ), | |
| ) | |
| return widget | |
| def nature_visualization(self): | |
| # Create a visualization of the nature of the dataframe | |
| widget = self.pn.Column( | |
| self.pn.pane.Markdown( | |
| """ | |
| # Data Nature | |
| ### Here is the nature of the dataset. | |
| """ | |
| ), | |
| self.pn.Row( | |
| self.pn.Column( | |
| self.pn.pane.Markdown("## Data Types"), | |
| self.pn.pane.DataFrame(self.df.dtypes.to_frame(),) | |
| ), | |
| self.pn.Column( | |
| self.pn.pane.Markdown("## Null Values"), | |
| self.pn.pane.DataFrame(self.df.isnull().sum().to_frame(),) | |
| ), | |
| ), | |
| ) | |
| return widget | |
| def distribution_visualization(self): | |
| # Create a visualization of the distribution of the dataframe | |
| widget = self.pn.Column( | |
| self.pn.pane.Markdown( | |
| """ | |
| # Data Distribution | |
| ### Here is the distribution of the dataset. | |
| """ | |
| ), | |
| self.pn.Column( | |
| self.pn.Column( | |
| self.pn.pane.Markdown("## Distribution"), | |
| self.df.hvplot.density() | |
| ), | |
| ), | |
| ) | |
| return widget |