5 Most Popular Data Science Code Snippets
Zack Shainsky
January 27, 2021
Are you tired of Googling the same code examples over and over again? So am I! That is why I use Code Snippets in Zepl. Below are my top 5 most used code examples that I have saved as Code Snippets in Zepl for fast and easy reuse.
- Loading packages
- Reading from a data source
- Visualizing data
- ML Functions
- (Bonus) Zepl Specific Functions
Reference Documentation
- Code Snippet Examples Below: Open in Zepl
- Code Snippet Documentation: https://new-docs.zepl.com/docs/using-the-zepl-notebook/develop/code-snippets
Loading Packages
Setting up your environment can be done programmatically in a Zepl Notebook or through our Custom Images feature. The code snippets below shows how to programmatically set up your environment and call Bash Shell commands:
%python
!pip install <package>
Connecting to data
The two most important functions when connecting to data are reading and writing. Below is an example of using Zepl’s Snowflake data source to read data to a Pandas DataFrame and display it using our built in function z.show(df).
Example of Reading from Snowflake: Open in Zepl
%python
import pandas as pd
cur = z.getDatasource(“TPCH_SF10”)
con = z.getDatasource(“TPCH_SF10_con”)
# cur.execute(“USE WAREHOUSE <warehouse>”)
cur.execute(“SELECT * FROM SNOWFLAKE_SAMPLE_DATA.TPCH_SF10.CUSTOMER LIMIT 100”)
df = cur.fetch_pandas_all()
z.show(df)
Visualizations
If your team has visualization functions to make styling or HTML/CSS/JS easier, this is a great application for Code Snippets. Below is one example of creating a Python function to display HTML/CSS for easy to view Single Value Charts:
%python
def card(title, value, secondValue = “”):
print(“””%html
<div style=”width: 200px;text-align:center;float:left;margin:10px 10px”>
<div style=”font-weight:bold;font-size:16px;color:#2BB5E9″>
{title}
</div>
<div style=’background-color:#62C1E2;color:white;padding:10px 10px; font-size: 30px’>
{value}
<div style=”font-size:15px”>{secondValue}</div>
</div>
</div>\n”””.format(title=title, value=value, secondValue=secondValue))print(“””
%html
<style>
.dataframe td {
padding: 5px 5px;
}
.dataframe th {
padding: 5px 5px;
}
.analytics {
font-size:24px;
color:#62C1E2;
}
.analyticsHL {
font-size:28px;
color:#2BB5E9;
font-weight:bold;
}
</style> “””)card(“Avg User Count”, 530, “”)
card(“Total Active Users”, 675, “”)
ML Functions
Data Prep/Cleanup/Feature engineering
If your team has a common data set that requires routine changes or prepared features, saving this logic as a code snippet, may save hours of wasted engineering time.
Forecasting models
At Zepl, we have been using either ARIMA or FB Prophet to forecast on financial data sets. I find myself constantly looking up examples of using these libraries, so i added it to my Code Snippets:
%python
m = Prophet()
ph_df = df.drop([‘OPEN’, ‘HIGH’, ‘LOW’,’VOLUME’, ‘SYMBOL’,’ADJCLOSE’,’ID’], axis=1)
ph_df.rename(columns={‘CLOSE’: ‘y’, ‘DATE’: ‘ds’}, inplace=True)m.fit(ph_df)
future_prices = m.make_future_dataframe(periods=365)
# Predict Prices
forecast = m.predict(future_prices)
forecast[[‘ds’, ‘yhat’, ‘yhat_lower’, ‘yhat_upper’]].tail()
Zepl specific functions
Zepl has specific built in functions that can be leveraged in our notebook interface. To avoid looking up references over and over again, add these to your Code Snippet drawer.
%python
# Checkbox Inputs
options = [(“apple”,”Apple”), (“banana”,”Banana”), (“orange”,”Orange”)]
list = z.checkbox(“Fruit”, options, [“apple”, “orange”])# Text Box inputs
z.textbox(“First Name”, “Zack”)# Multi-Select inputs
day = z.select(“Day”, [(“1″,”mon”),
(“2″,”tue”),
(“3″,”wed”),
(“4″,”thur”),
(“5″,”fri”),
(“6″,”sat”),
(“7″,”sun”)])