4

Here's what i have so far:

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)

How can i replace each column of the dataframe, data2 with the corresponding column in the array, Xrescaled? Thanks.

1 Answer 1

5

You can just do: data2.iloc[:,:8] = Xrescaled, here is a demo:

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Psidom. I ran into a couple of problems around my original assumptions regarding "data2 = pd.DataFrame(data) # copy of data", which i changed to "data2 = copy.deepcopy(data) # pandas.pydata.org/pandas-docs/stable/generated/… " because of reference issues, However, your solution WORKED FOR ME. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.