Skip to content

Commit 5440c88

Browse files
committed
updating perfplot code to make nicer graphs
1 parent 49dd163 commit 5440c88

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

pandas-iterate-over-rows/cumulative_sum_perfplot.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,23 @@ def pandas_cumsum(products):
2020
).drop(columns="income")
2121

2222

23-
products = pd.read_csv("resources/products.csv")
23+
def get_products(n):
24+
products = pd.read_csv("resources/products.csv")
25+
if n < len(products):
26+
return products.iloc[:n]
27+
return pd.concat([products for _ in range((n // len(products)) + 1)]).iloc[
28+
:n
29+
]
30+
2431

2532
plot = perfplot.bench(
26-
n_range=[i**2 for i in range(1, 1000, 100)],
27-
setup=lambda n: pd.concat([products for _ in range(n)]),
33+
n_range=[2**i for i in range(20)],
34+
setup=get_products,
2835
kernels=[pandas_cumsum, loop_cumsum],
2936
labels=["pandas cumsum", "loop cumsum"],
3037
equality_check=None,
38+
title="Loop vs Pandas Cumulative Sum",
39+
xlabel="Number of Rows",
3140
)
3241

3342
plot.show()
34-
plot.show(logy=True)

pandas-iterate-over-rows/take_sum_perfplot.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,22 @@ def python_sum(websites):
1717
return sum(row.total_views for row in websites.itertuples())
1818

1919

20-
websites = pd.read_csv("resources/popular_websites.csv", index_col=0)
20+
def get_websites(n):
21+
websites = pd.read_csv("resources/popular_websites.csv", index_col=0)
22+
if n < len(websites):
23+
return websites.iloc[:n]
24+
return pd.concat([websites for _ in range((n // len(websites)) + 1)]).iloc[
25+
:n
26+
]
27+
2128

2229
plot = perfplot.bench(
23-
n_range=[i**2 for i in range(1, 1000, 100)],
24-
setup=lambda n: pd.concat([websites for _ in range(n)]),
30+
n_range=[2**i for i in range(17)],
31+
setup=get_websites,
2532
kernels=[pandas_sum, loop_sum, python_sum],
2633
labels=["pandas sum", "loop sum", "python sum"],
34+
title="Python vs Pandas sum",
35+
xlabel="Number of Rows",
2736
)
2837

2938
plot.show()
30-
plot.show(logy=True)

0 commit comments

Comments
 (0)