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
# data - pandas dataframe | |
def missing_value_describe(data): | |
# check missing values in the data | |
total = data.isna().sum().sort_values(ascending=False) | |
missing_value_pct_stats = (data.isnull().sum() / len(data)*100) | |
missing_value_col_count = sum(missing_value_pct_stats > 0) | |
# missing_value_stats = missing_value_pct_stats.sort_values(ascending=False)[:missing_value_col_count] | |
missing_data = pd.concat([total, missing_value_pct_stats], axis=1, keys=['Total', 'Percent']) |
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 | |
# using pandas datetime converter | |
df = pd.DataFrame(pd.Series(['2021-02-04T09:15:00+05:30', '2021-02-04T09:15:00+05:30', '2021-02-04T09:15:00+05:30'])) | |
pd.to_datetime(df[0]).dt.date | |
pd.to_datetime(df[0]).dt.time | |
# using dateutil library | |
from dateutil.parser import parse | |
df[0].map(lambda x: parse(x).date()) |
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
# Install qrcode https://pypi.org/project/qrcode/ | |
# pip install qrcode[pil] | |
import qrcode | |
qr = qrcode.QRCode( | |
version=1, | |
error_correction=qrcode.constants.ERROR_CORRECT_L, | |
box_size=10, | |
border=4, | |
) |
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
# input | |
import os | |
for dirname, _, filenames in os.walk('/files/input'): | |
for filename in filenames: | |
print(os.path.join(dirname, filename)) |
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
""" | |
requests with session gives faster results | |
the below code demonstrates a comparison in speed between requests with session vs requests not with session | |
""" | |
import requests | |
import datetime | |
from bs4 import BeautifulSoup | |
def scrape_no_session(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
""" | |
Suppose you have a django model in models.py: | |
class Animal(models.Model): | |
animal_name = models.CharField(u'name', max_length=255) | |
to create an alphabetfilter in django admin panel you need to create a class which inherits | |
admin.SimpleListFilter in admin.py | |
here is the code snippet below for admin.py file |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 base64 | |
def download_dataset(dataset): | |
csv = dataset.to_csv(index=False) | |
b64 = base64.b64encode(csv.encode()).decode() # strings to bytes conversions | |
href_link = f'<a href="data:file/csv;base64,{b64}" download="player_stats.csv">Download CSV File</a>' | |
return href_link | |
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
# sample Url https://www.basketball-reference.com/leagues/NBA_2021_per_game.html | |
import pandas as pd | |
def parse_data(year: str): | |
url = "https://www.basketball-reference.com/leagues/NBA_" + year + "_per_game.html" | |
parsed_df = pd.read_html(url, header=0)[0] | |
parsed_df = parsed_df.drop(parsed_df[parsed_df['Age'] == 'Age'].index) # to remove duplicate headers | |
parsed_df = parsed_df.fillna(0) | |
parsed_df = parsed_df.drop(['Rk'], axis=1) # to index(Rk column) |
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 simple regex approach to find specific portion (in this case post id) from a http link | |
''' | |
import re | |
link = "https://www.facebook.com/marketplace/item/391273608941747/?ref=search&referral_code=undefined" | |
print(re.findall("marketplace/item/(\S*)/", str(link))[0]) | |
# output |