Trực quan hóa dữ liệu chuỗi thời gian trong Python
Thomas Vincent
Head of Data Science, Getty Images
# Khởi tạo một dictionary trong Python
my_dict = {}
# Thêm một key và value vào dictionary
my_dict['your_key'] = 'your_value'
# Thêm key và value thứ hai vào dictionary
my_dict['your_second_key'] = 'your_second_value'
# In dictionary ra
print(my_dict)
{'your_key': 'your_value',
'your_second_key': 'your_second_value'}
# Import thư viện statsmodel
import statsmodels.api as sm
# Khởi tạo một dictionary
my_dict = {}
# Lấy tên các chuỗi thời gian
ts_names = df.columns
print(ts_names)
['ts1', 'ts2', 'ts3']
# Chạy phân rã chuỗi thời gian
for ts in ts_names:
ts_decomposition = sm.tsa.seasonal_decompose(jobs[ts])
my_dict[ts] = ts_decomposition
# Khởi tạo một dictionary mới
my_dict_trend = {}
# Trích xuất thành phần xu hướng
for ts in ts_names:
my_dict_trend[ts] = my_dict[ts].trend
# Chuyển thành 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
...
Trực quan hóa dữ liệu chuỗi thời gian trong Python