Skip to content

Commit 87758dd

Browse files
authored
Merge pull request #324 from realpython/power-bi-python
Power BI: Initial commit
2 parents ea5be02 + ed72566 commit 87758dd

File tree

7 files changed

+55
-0
lines changed

7 files changed

+55
-0
lines changed

power-bi-python/01_ingest.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import sqlite3
2+
3+
import pandas as pd
4+
5+
with sqlite3.connect(r"C:\<YOUR_PATH_TO_DB>\car_sales.db") as connection:
6+
df = pd.read_sql_query("SELECT * FROM sales", connection)
7+
8+
cars = df[
9+
[
10+
"vin",
11+
"car",
12+
"mileage",
13+
"license",
14+
"color",
15+
"purchase_date",
16+
"purchase_price",
17+
"investment",
18+
]
19+
]
20+
customers = df[["vin", "customer"]]
21+
sales = df[["vin", "sale_price", "sale_date"]]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# flake8: noqa
2+
# 'dataset' holds the input data for this script
3+
dataset = dataset.assign(
4+
full_name=dataset["customer"].str.extract(r"([^<]+)"),
5+
email=dataset["customer"].str.extract(r"<([^>]+)>"),
6+
).drop(columns=["customer"])
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# flake8: noqa
2+
# 'dataset' holds the input data for this script
3+
dataset[["first_name", "last_name"]] = dataset["full_name"].str.split(
4+
n=1, expand=True
5+
)
6+
dataset.drop(columns=["full_name"], inplace=True)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# flake8: noqa
2+
# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script:
3+
4+
# dataset = pandas.DataFrame(color, vin)
5+
# dataset = dataset.drop_duplicates()
6+
7+
# Paste or type your script code here:
8+
import matplotlib.pyplot as plt
9+
10+
plt.style.use("seaborn")
11+
12+
series = dataset[dataset["color"] != ""]["color"].value_counts()
13+
series.plot(kind="bar", color=series.index, edgecolor="black")
14+
15+
plt.show()

power-bi-python/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Microsoft Power BI and Python: Two Superpowers Combined
2+
3+
These are supplemental materials for the Real Python [tutorial](https://realpython.com/power-bi-python/) on using Python in Microsoft Power BI Desktop. They include:
4+
5+
- SQLite database with a single `sales` table (fake used car sales dataset)
6+
- Python scripts used in the tutorial
7+
- The resulting Power BI Desktop report

power-bi-python/car_sales.db

148 KB
Binary file not shown.

power-bi-python/report.pbix

198 KB
Binary file not shown.

0 commit comments

Comments
 (0)