Given a NCBI feature table that lists coding sequences, just wanted to calculate length of non-coding regions with pandas. This is obviously easy when you iterate a dataframe, but that can be painfully slow. Instead, here's how to do it in a "pandas-like" way.
def calc_nc_len(df):
# Before and after here merely means coordinates on the genome disregarding the coding strand of each gene
df["before"] = df["end"].diff()
df["before"] -= df.apply(lambda x: x["end"] - x["start"],axis=1)
df["after"] = -df["start"].diff(periods=-1)
df["after"] -= df.apply(lambda x: x["end"] - x["start"],axis=1)