简体   繁体   中英

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?

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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM