简体   繁体   English

如何使用 pandas 将附加列表中的数据插入 csv 文件?

[英]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":如果您的predictionshistory是“附加列表”:

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.

相关问题 如何使用pandas.read_csv将CSV文件中的数据插入数据框? - How can I insert data from a CSV file into a dataframe using pandas.read_csv? 如何从json文件中读取数据,并使用熊猫将其转换为csv? - How to read data from json file and convert it to csv using pandas? 如何将 csv 文件中的数据插入 treeview? - How to insert data from csv file to treeview? Python pandas从csv文件中读取列表数据类型中的列表 - Python pandas read list in list data type from csv file 如何将熊猫数据框写入csv文件? 如何从最终字典中删除列表数组大括号([])? - how to write pandas data frame into csv file? How to remove list array braces ([]) from final dictionary? 如何从csv文件读取数据并将数据插入html文件 - How to read in data from a csv file and insert the data into an html file 从 sql 文件或使用 csv 文件将数据插入 mysql - Insert data into mysql from sql file or using csv file 无法使用 pandas 从 csv 文件访问列表 - Unable to access list from csv file using pandas 使用 SQL 服务器使用 Pandas 的数据创建 CSV 文件 - Creating a CSV file using data from SQL Server using Pandas 如何在不使用 pandas dataframe 的情况下从 CSV 文件中提取列值列表 - How to extract a list of column values from a CSV file without using a pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM