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
def check_nulls(df): | |
df_cols = df.columns | |
col_counts = [df[col].count() for col in df_cols] | |
col_lens = [len(df[col]) for col in df_cols] | |
cdf = pd.DataFrame(index = df_cols, | |
data = {'Values':col_counts, | |
'Total': col_lens}) | |
cdf['% N/A'] = 1-(cdf['Values']/cdf.Total) | |
cdf['% N/A'] = cdf['% N/A'].map('{:.1%}'.format) # formats as percentages | |
return cdf |
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
# from https://stackoverflow.com/a/44444489/7471215 | |
# puts commas in integers | |
ax.set_yticklabels(['{:,}'.format(int(x)) for x in ax.get_yticks().tolist()]) | |
# formats percentages | |
ax.set_yticklabels(['{:.0%}'.format(x) for x in ax.get_yticks().tolist()]) |
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
flat_list = [item for sublist in nested_list for item in sublist] | |
# if a list is variable, like [2,3,[2,3],[2,3]], you'll need a function | |
# this creates a generator that will do what you want | |
def flatten(lis): | |
for item in lis: | |
if isinstance(item, list) and not isinstance(item, str): | |
for x in flatten(item): | |
yield x | |
else: |
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
# multiwindow | |
"C:\Program Files\VcXsrv\vcxsrv.exe" :0 -ac -terminate -lesspointer -multiwindow -clipboard -wgl -dpi auto |
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
import pandas as pd | |
import requests | |
from io import BytesIO | |
# using a 538 dataset as an example | |
url = 'https://raw.githubusercontent.com/fivethirtyeight/data/master/bob-ross/elements-by-episode.csv' | |
response = requests.get(url) | |
content = BytesIO(response.content) | |
df = pd.read_csv(content) |
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
a = numpy.array([0, 3, 0, 1, 0, 1, 2, 1, 0, 0, 0, 0, 1, 3, 4]) | |
unique, counts = numpy.unique(a, return_counts=True) | |
dict(zip(unique, counts)) # returns {0: 7, 1: 4, 2: 1, 3: 2, 4: 1} |
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
# Pandas settings to include on import | |
import pandas as pd | |
import numpy as np | |
pd.set_option('display.max_rows',1000) | |
pd.set_option('display.max_columns',1000) | |
# Includes commas in outputs > 1,000, and formats as integers if integers | |
# If not integers, formats to two decimal places | |
pd.set_option('display.float_format', lambda x: "{:,.0f}".format(x) if x.is_integer() | |
else "{:,.2f}".format(x)) |
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
// Use Gists to store code you would like to remember later on | |
console.log(window); // log the "window" object to the console |
NewerOlder