mo1996
mo1996

Reputation: 1

Adding dataframe column to numpy.array

For a regression, I would like to add a dataframe column to a numpy.array which contains dummy variables.

Currently, the array looks like this:

[[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]
 [0 0 0 1]
 [0 0 1 0]]

I would like to add the dataframe column values (which has 7 rows in this example) so that 5 values are inside the square brackets (the one from the dataframe column and four dummy variables).

Does anyone know how to solve this?

Upvotes: 0

Views: 136

Answers (1)

pls78
pls78

Reputation: 11

You could use your numpy array to create a dataframe:

array=np.array([[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 1],[0, 0, 1, 0]])
new_dataframe = pd.DataFrame(data=array)

and then add your column to it like this:

new_dataframe['4'] = your_dataframe['column_name']

Upvotes: 1

Related Questions