Extract file name from path in Python

import ntpath
import os

filename = "templates - original/001.png"
print(ntpath.basename(filename))
print(os.path.basename(filename))
print(os.path.split(filename))
print(os.path.splitext(filename))
print(os.path.splitext(os.path.basename(filename)))
print(os.path.splitext(os.path.basename(filename))[0])

output

001.png
001.png
('templates - original', '001.png')
('templates - original/001', '.png')
('001', '.png')
001

References
https://stackoverflow.com/questions/8384737/extract-file-name-from-path-no-matter-what-the-os-path-format

Iterate over rows in a DataFrame in Pandas

# import pandas package as pd 
import pandas as pd 
  
# Define a dictionary containing students data 
data = {'Name': ['Ankit', 'Amit', 'Aishwarya', 'Priyanka'], 
                'Age': [21, 19, 20, 18], 
                'Stream': ['Math', 'Commerce', 'Arts', 'Biology'], 
                'Percentage': [88, 92, 95, 70]} 
  
# Convert the dictionary into DataFrame 
df = pd.DataFrame(data, columns = ['Name', 'Age', 'Stream', 'Percentage']) 
  
print("Given Dataframe :\n", df) 
  
print("\nIterating over rows using iterrows() method :\n") 
  
# iterate through each row and select  
# 'Name' and 'Age' column respectively. 
for index, row in df.iterrows(): 
    print (row["Name"], row["Age"])

References
https://www.geeksforgeeks.org/different-ways-to-iterate-over-rows-in-pandas-dataframe/

Box Annotations in Bokeh

from bokeh.sampledata.glucose import data
from bokeh.plotting import figure, show, output_file
from bokeh.models import BoxAnnotation

output_file("box_annotation.html", title="box_annotation.py example")

TOOLS = "pan,wheel_zoom,box_zoom,reset,save"

#reduce data size
data = data.loc['2010-10-06':'2010-10-13']

p = figure(x_axis_type="datetime", tools=TOOLS)

p.line(data.index.to_series(), data['glucose'], line_color="gray", line_width=1, legend_label="glucose")

low_box = BoxAnnotation(top=80, fill_alpha=0.1, fill_color='red')
mid_box = BoxAnnotation(bottom=80, top=180, fill_alpha=0.1, fill_color='green')
high_box = BoxAnnotation(bottom=180, fill_alpha=0.1, fill_color='red')

p.add_layout(low_box)
p.add_layout(mid_box)
p.add_layout(high_box)

p.title.text = "Glucose Range"
p.xgrid[0].grid_line_color=None
p.ygrid[0].grid_line_alpha=0.5
p.xaxis.axis_label = 'Time'
p.yaxis.axis_label = 'Value'

show(p)

References
https://docs.bokeh.org/en/latest/docs/user_guide/annotations.html#box-annotations