您可以从附加列表中创建一个新的 DataFrame,然后将其与现有列表合并。我认为最简单的方法是,如果您将数据作为还包含日期索引的字典列表提供。即类似的东西(我假设df_original
是具有原始值的数据框):
import datetime
import pandas
# Calculating your predictions (this should be replaced with an
# appropriate algorithm that matches your case)
latest_entry = df_original.iloc[-1]
latest_datetime = latest_entry['date']
# We assume lastest_datetime is a Python datetime object.
# The loop below will create 10 predictions. This should be adjusted to make
# sense for your program. I'm assuming the function `compute_prediction()`
# will generate the predicted value. Again, this you probably want to tweak
# to make it work in your program :)
# The computed predictions will be stored inside a list of dicts.
predictions = list()
for _ in range(10):
predicted_date = latest_datetime + datetime.timedelta(hours=1)
predicted_value = compute_prediction()
tmp_dict = {
'date': predicted_date, 'Outbound': predicted_value
}
predictions.append(tmp_dict)
# Convert the list of dictionaries into a data frame.
df_predictions = pandas.DataFrame.from_dict(predictions)
# Append the values of your new data frame to the original one.
df_concatenated = pandas.concat(df_original, df_predictions)
当然, 中date
使用的密钥predictions
需要与原始数据框中使用的密钥类型相同。结果df_concatenated
将有两个数据帧在一起。要绘制结果,您可以调用df_concatenated.plot()
(或调用所需的适当绘图函数)。
您可以在此处找到有关合并多个数据框的更多详细信息。