Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a dataframe column with a numpy array column in Python?

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.

like image 754
Love and peace - Joe Codeswell Avatar asked Sep 07 '25 18:09

Love and peace - Joe Codeswell


1 Answers

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
like image 97
Psidom Avatar answered Sep 10 '25 13:09

Psidom