Skip to content

Instantly share code, notes, and snippets.

@timothymugayi
Last active December 6, 2019 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timothymugayi/ce1383176ecfe79a0ac21aee55f2dba1 to your computer and use it in GitHub Desktop.
Save timothymugayi/ce1383176ecfe79a0ac21aee55f2dba1 to your computer and use it in GitHub Desktop.
Example how to display a progress bar with tqdm in Pandas Dataframe
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