Can I save prediction value in same csv file as a another column using panda python I have csv data file and I design LSTM model to predict values. Then I want to save that prediction value in same csv file. Can I do that? I tried using one code then in my csv file only had prediction values and delete other columns. Can anyone give me a suggestion for that.

import pandas as pd
import numpy as np
data = pd.read_csv('data1.csv')
data = pd.DataFrame(data,columns=['x','x1','x2','y'])
data.columns = ['x', 'x1', 'x2','y']
pd.options.display.float_format = '{:,.0f}'.format
data = data.dropna ()
d = ['y']
y=data['y'].astype(int)
cols=['x', 'x1', 'x2']
x=data[cols].astype(int)
scaler_x = preprocessing.MinMaxScaler(feature_range =(-1, 1))
x = np.array(x).reshape ((len(x),3 ))
x = scaler_x.fit_transform(x)
scaler_y = preprocessing.MinMaxScaler(feature_range =(-1, 1))
y = np.array(y).reshape ((len(y), 1))
y = scaler_y.fit_transform(y)
print("row",len(y))
n = data.shape[0]
p = data.shape[1]
fill_missing(data.values)
train_start = 0
train_end = int(np.floor(0.65*n))
test_start = train_end+1 
test_end = n
x_train = x[np.arange(train_start, train_end), :]
x_test = x[np.arange(test_start, test_end), :]
y_train = y[np.arange(train_start, train_end), :]
y_test = y[np.arange(test_start, test_end), :]
x_train=x_train.reshape(x_train.shape +(1,))
x_test=x_test.reshape(x_test.shape + (1,))
seed = 20
np.random.seed(seed)
fit1 = Sequential ()
fit1.add(LSTM(
output_dim = 10,
activation='relu',
input_shape =(3,1)))
fit1.add(Dense(output_dim =1))
fit1.add(Activation(linear))
batchsize = 10
fit1.compile(loss="mean_squared_error",optimizer="adam")
fit1.fit(x_train , y_train , batch_size = batchsize, nb_epoch =10,   shuffle=True)
print(fit1.summary ())
pred1=fit1.predict(x_test)
pred1=fit1.predict(x_test)
real_test = scaler_y.inverse_transform(np.array(y_test).reshape ((len(y_test), 1))).astype(int)
pred1 = pd.DataFrame(pred1, columns=['pred1']).to_csv('data1.csv')
if you want this column in the same dataframe just do

data['pred'] = pred1
data.to_csv('data1.csv')


The first line automatically adds a column called 'pred' to the dataframe with values coming from pred1.

Hope it helps. Good luck!