[英]How to insert data from a appened list to the csv file using pandas?
I have a predicted data which is scraped from a website and applied ARIMA algorithm in the form below我有一个从网站上抓取的预测数据,并以下面的形式应用了 ARIMA 算法
predicted=12891.806866, expected=12890.000000
predicted=12889.863342, expected=12890.000000
predicted=12880.874762, expected=12890.000000
.....
......
........
predicted=10453.575744, expected=10999.000000
predicted=10873.639037, expected=10999.000000
predicted=11021.455329, expected=10490.000000
predicted=10620.855937, expected=10490.000000
I got this data by below snippet我通过以下片段获得了这些数据
for t in range(len(test)):
model = arima_model.ARIMA(history, order=(10,1,0))
model_fit = model.fit(disp=0)
output = model_fit.forecast()
yhat = output[0]
predictions.append(yhat)
obs = test[t]
history.append(obs)
print("predicted=%f, expected=%f" % (yhat, obs))
print(predictions) #returns only single value [array([12755.95876035])]
print(history) #returns [15095, 14890, 14890, 14449, 14449, 14449, 14890, 14890, 14890, 14890, 14890, 14890, 14890, ....................................... ,12990, 12990, 12990, 12990, 12990]
When i try to add this data to the existing column in the csv file i dont really see all the data filling in all the rows.当我尝试将此数据添加到 csv 文件中的现有列时,我并没有真正看到所有数据都填充在所有行中。 I tried with different techniques which is know.我尝试了不同的已知技术。 But i failed miserably.但我失败得很惨。 I realized that i am not reading/copying the data in proper way to insert in the csv file.我意识到我没有以正确的方式读取/复制数据以插入 csv 文件。 The techniques which i written below are simple examples i tried我在下面编写的技术是我尝试过的简单示例
1.
df = pd.read_csv("mydata.csv")
df['Predicted'] = yhat
df['Expected'] = obs
df.to_csv("mydata.csv")
2.
with open("mydata.csv", "w") as data:
w = csv.DictWriter(data, fieldnames={"Predicted", "Expected"})
w.writeheader()
w.writerow({'Predicted': yhat, 'Expected': obs})
#Even w.writerows didn't really help
3.
df.insert(value=yhat, column="Predicted", allow_duplicates = True)
RESULT:
Empty DataFrame
Columns: [Expected, Predicted]
Index: []
i ended up with column filled up with only one row from the above print("predicted=%f, expected=%f" % (yhat, obs))
I want to fill mydata.csv with all predicted and expected values.我想用所有预测值和预期值填充 mydata.csv 。 I knew that i am missing some technique here.我知道我在这里缺少一些技术。 It would be great if someone helps me.如果有人帮助我,那就太好了。
Thanks in Advance!提前致谢!
If your predictions
and history
are the "appended lists":如果您的predictions
和history
是“附加列表”:
df = pd.read_csv("mydata.csv")
df['Predicted'] = predictions
df['Expected'] = history
df.to_csv("mydata.csv")
I am a bit happy to answer my own question.我有点高兴回答我自己的问题。 Bringing for loop after opening the csv file 'with' worked.在打开 csv 文件“with”后进行循环。
with open('mydata.csv', 'a+') as csvfile:
fieldnames = ['Expected', 'Predicted']
w = csv.DictWriter(csvfile, fieldnames=fieldnames)
w.writeheader()
for t in range(len(test)):
model = ARIMA(history, order=(10, 1, 0))
model_fit = model.fit()
output = model_fit.forecast()
yhat = output[0]
predictions.append(yhat)
obs = test[t]
history.append(obs)
print("predicted=%f, expected=%f" % (yhat, obs))
w.writerow({'Expected' : obs, 'Predicted' : yhat})
df_predicted = pd.read_csv("mydata.csv")
print(df_predicted)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.