Visualizing Time Series Data in Python
Thomas Vincent
Head of Data Science, Getty Images
# Initialize a Python dictionnary
my_dict = {}
# Add a key and value to your dictionnary
my_dict['your_key'] = 'your_value'
# Add a second key and value to your dictionnary
my_dict['your_second_key'] = 'your_second_value'
# Print out your dictionnary
print(my_dict)
{'your_key': 'your_value',
'your_second_key': 'your_second_value'}
# Import the statsmodel library
import statsmodels.api as sm
# Initialize a dictionary
my_dict = {}
# Extract the names of the time series
ts_names = df.columns
print(ts_names)
['ts1', 'ts2', 'ts3']
# Run time series decomposition
for ts in ts_names:
ts_decomposition = sm.tsa.seasonal_decompose(jobs[ts])
my_dict[ts] = ts_decomposition
# Initialize a new dictionnary
my_dict_trend = {}
# Extract the trend component
for ts in ts_names:
my_dict_trend[ts] = my_dict[ts].trend
# Convert to a DataFrame
trend_df = pd.DataFrame.from_dict(my_dict_trend)
print(trend_df)
ts1 ts2 ts3
datestamp
2000-01-01 2.2 1.3 3.6
2000-02-01 3.4 2.1 4.7
...
Visualizing Time Series Data in Python