-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
{ | ||
"config": { | ||
"view": { | ||
"continuousWidth": 400, | ||
"continuousHeight": 300 | ||
}, | ||
"axis": { | ||
"labelFontSize": 11 | ||
} | ||
}, | ||
"layer": [ | ||
{ | ||
"mark": "bar", | ||
"encoding": { | ||
"x": { | ||
"type": "nominal", | ||
"axis": { | ||
"title": "" | ||
}, | ||
"field": "variable", | ||
"sort": "-y" | ||
}, | ||
"y": { | ||
"type": "quantitative", | ||
"axis": { | ||
"title": "Count of nulls" | ||
}, | ||
"field": "value" | ||
} | ||
}, | ||
"title": "" | ||
}, | ||
{ | ||
"mark": { | ||
"type": "text", | ||
"baseline": "bottom" | ||
}, | ||
"encoding": { | ||
"text": { | ||
"type": "nominal", | ||
"field": "percentage" | ||
}, | ||
"x": { | ||
"type": "nominal", | ||
"axis": { | ||
"title": "" | ||
}, | ||
"field": "variable", | ||
"sort": "-y" | ||
}, | ||
"y": { | ||
"type": "quantitative", | ||
"axis": { | ||
"title": "Count of nulls" | ||
}, | ||
"field": "value" | ||
} | ||
}, | ||
"title": "" | ||
} | ||
], | ||
"data": { | ||
"name": "data-0e7bce5a1d2f132e282789d6ef7780fe" | ||
}, | ||
"width": 1250, | ||
"$schema": "https://vega.github.io/schema/vega-lite/v4.8.1.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from pyspark.sql.dataframe import DataFrame | ||
from pyspark.sql.functions import col, count, when | ||
|
||
from .charts import load_chart_definition, altair_if_installed_else_json | ||
|
||
import pandas as pd | ||
|
||
|
||
def missingness_chart(df: DataFrame): | ||
"""Produce bar chart of missingness in standardised nodes | ||
Args: | ||
df (DataFrame): Input Spark dataframe | ||
Returns: | ||
Bar chart of missingness | ||
""" | ||
|
||
# Load JSON definition of missingness chart | ||
chart_path = "missingness_chart_def.json" | ||
missingness_chart_def = load_chart_definition(chart_path) | ||
|
||
|
||
# Data for plot | ||
# Count and percentage of nulls in each columns as pandas dataframe | ||
df_nulls = df.select([count(when(col(c).isNull(), c)).alias(c) for c in df.columns]) | ||
pd_nulls = df_nulls.toPandas() | ||
pd_nulls = pd.melt(pd_nulls) | ||
|
||
record_count = df.count() | ||
pd_nulls["percentage"] = round(pd_nulls['value']*100/record_count, 1) | ||
pd_nulls["percentage"] = pd_nulls['percentage'].astype(str) + "%" | ||
|
||
|
||
# Add data to JSON chart definition | ||
missingness_chart_def["data"]["values"] = pd_nulls.to_dict("records") | ||
|
||
# Update chart title | ||
for c in missingness_chart_def["layer"]: | ||
c["title"] = "Missingness per column out of {} records".format(record_count) | ||
|
||
|
||
return altair_if_installed_else_json(missingness_chart_def) |