Skip to content Skip to sidebar Skip to footer

Adding Stats Code To A Function In Python

Im relatively new to Python and trying to learn how to write functions. The answer to this post highlights how to get certain stats from a dataframe and I would like to use it in

Solution 1:

Here you go:

def repeat_stats(series, var):
    isvar = series == var
    wasntvar = series != series.shift()
    cont_grps = (isvar & wasntvar).cumsum()
    counts = isvar.loc[cont_grps.astype(bool)].groupby(cont_grps).sum()
    return counts.value_counts() / cont_grps.max()

repeat_stats(rng.initial_data, 'B')

3.0    0.5
2.0    0.5
Name: initial_data, dtype: float64

Post a Comment for "Adding Stats Code To A Function In Python"