Last active
December 6, 2019 15:34
-
-
Save timothymugayi/ce1383176ecfe79a0ac21aee55f2dba1 to your computer and use it in GitHub Desktop.
Example how to display a progress bar with tqdm in Pandas Dataframe
This file contains hidden or 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 time | |
import pandas as pd | |
import requests | |
from tqdm import tqdm | |
def percent_off(product_price, discount): | |
try: | |
discount = float(discount) | |
if discount < 0 and discount > 100: | |
raise ValueError('discout amount should be between 1 and 100%') | |
value = (product_price - (product_price * (discount / 100.0))) | |
time.sleep(0.0001) | |
return value | |
except ValueError as e: | |
print('invalid product_price or discount amount', e) | |
raise e | |
def appy_discount(perentage): | |
df = pd.DataFrame(pd.read_json('products.json')) | |
df.insert(4, 'discount', 0) | |
tqdm.pandas(desc='apply_{}_percent_off'.format(perentage)) | |
df['discount'] = df['price'].progress_apply(lambda x: percent_off(x, perentage)) | |
return df | |
# Downlaod sample best buy products json file | |
# It sucks right that you do not see a progress bar while downloadng this large file below | |
r = requests.get('https://github.com/BestBuyAPIs/open-data-set/raw/master/products.json', allow_redirects=True) | |
open('products.json', 'wb').write(r.content) | |
# How about now imagine performing a large pandas dataframe calculation | |
df = appy_discount(5) | |
df # use this to a nice html output in jupyter notebooks else print to sysout |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment