title: Join array to dataframe in python
body: <p>I am doing so predictive modeling</p>
  
  <p>as usual</p>
  
  <p>splitting data into <strong>x_train, x_test, y_train, y_test</strong></p>
  
  <p>and then getting the test prediction in <strong>y_pred</strong></p>
  
  <p>once I finish I want to put data into a <strong>csv</strong> file</p>
  
  <p>but the problem is when I try to join <strong>y_pred</strong> to <strong>y_test</strong>, it does not join as expected.</p>
  
  <p>will get something like this</p>
  
  <pre><code>    Class Data    TotalCnt  0
  16  3     2209    5235      
  98  3     2190    4871      
  07  1     2183    1342      1690
  09  1     2205    1540      1540
  19  3     2191    4673      
  01  1     2206    3117      1005
  38  3     2200    4837      
  44  3     2219    4965      
  04  1     2195    1340      1690
  10  1     2191    1980      2002
  38  3     2184    4620      
  15  3     2220    4781      
  18  3     2223    4872      
  </code></pre>
  
  <p>it deletes some records</p>
  
  <p>I think the cause of the problem is the following </p>
  
  <p><strong>y_pred</strong> is predictions for random set from the original dataframe
  so it should look like this</p>
  
  <pre><code>ID      Prediction
  16      1005
  98      2056
  07      1690
  54      1690
  ...
  .
  .
  .
  </code></pre>
  
  <p><strong>y_pred</strong> is an array, so in order for me to join it with <strong>x_test</strong> I convert it to dataframe</p>
  
  <p>once <strong>y_pred</strong> converted to a dataframe, it loses the IDs, so it become sequential <code>1, 2, 3, 4, ...</code></p>
  
  <pre><code>ID      Prediction
  1       1005
  2       2056
  3       1690
  4       1690
  ...
  .
  .
  .
  </code></pre>
  
  <p>therefore when try to join with x_test it only matches the ID numbers that exist in both dataframes <strong>x_test</strong> and <strong>y_pred</strong></p>
  
  <p>How can I get the predictions as dataframe instead of array</p>
  
  <p>I am using this</p>
  
  <pre><code>x_train, x_test, y_train, y_test = train_test_split(x,y)
  rf = RandomForestRegressor(n_estimators=10000)
  rf.fit(x_train, y_train) 
  y_pred = rf.predict(x_test)
  
  . . .
  . . .
  
  def Lead0(value):
          return "0" + str(value) if value &lt; 10 else str(value)
  
  dNow = datetime.datetime.now()
  sNow = Lead0(dNow.year) + Lead0(dNow.month) + Lead0(dNow.day) + Lead0(dNow.hour) + Lead0(dNow.minute) + Lead0(dNow.second) 
  
  y_pred = pd.DataFrame(y_pred)
  y_out = x_test
  y_out = y_out.join(y_test)
  y_out = y_out.join(y_pred)
  
  y_out.to_csv(sFolder + "dfPred__" + sNow +".csv")
  </code></pre>
  
  <p>How to join array to dataframe without losing the ID order</p>
  
  <p>How to convert array to dataframe without losing the ID order</p>