Untitled

 avatar
unknown
plain_text
15 days ago
824 B
3
Indexable
# Fill in the complete data table including years where moving average can't be computed
full_df = pd.DataFrame({
    "Year": list(range(1992, 2001)),
    "Students": [1332, 1397, 1457, 1592, 1662, 1805, 1910, 2027, 2050]
})

# Add 5-Year Total and Average with NaN where not computable
full_df['5-Year Total'] = full_df['Students'].rolling(window=5).sum()
full_df['5-Year Average'] = full_df['Students'].rolling(window=5).mean()
full_df['Center Year'] = full_df['Year'].rolling(window=5).apply(lambda x: int(x.iloc[2]))

# Replacing NaN with '-' for display purposes
display_df = full_df.copy()
display_df[['5-Year Total', '5-Year Average', 'Center Year']] = display_df[['5-Year Total', '5-Year Average', 'Center Year']].fillna('-')

# Convert to string for cleaner formatting
display_df = display_df.astype(str)
display_df
Editor is loading...
Leave a Comment