How to replace a dataframe column with a numpy array column in Python?
<p>Here's what i have so far:</p>

<pre><code>data.shape:     # data == my dataframe
(768, 9)
data2 = pd.DataFrame(data)   # copy of data

array = data.values      # convert data to arrays
X = array[:,0:8]
Y = array[:,8]

# perform a transformation on X
Xrescaled = scaler.transform(X)
</code></pre>

<p>How can i replace each column of the dataframe, <code>data2</code> with the <strong>corresponding</strong> column in the array, <code>Xrescaled</code>? Thanks.</p>

<p>You can just do: <code>data2.iloc[:,:8] = Xrescaled</code>, here is a demo:</p>

<pre><code>import numpy as np
data = pd.DataFrame({'x': [1,2], 'y': [3,4], 'z': [5,6]})

data
#   x   y   z
#0  1   3   5
#1  2   4   6

import pandas as pd
data2 = pd.DataFrame(data)

data2
#   x   y   z
#0  1   3   5
#1  2   4   6    

X = data.values[:,:2]
Xrescaled = X * 2

Xrescaled
# array([[2, 6],
#        [4, 8]])

data2.iloc[:,:2] = Xrescaled
data2
#   x   y   z
#0  2   6   5
#1  4   8   6
</code></pre>

python arrays pandas numpy dataframe