Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

I have a Pandas data frame object of shape (X,Y) that looks like this:

[[1, 2, 3],

[4, 5, 6],

[7, 8, 9]]

and a numpy sparse matrix (CSC) of shape (X,Z) that looks something like this

[[0, 1, 0],

[0, 0, 1],

[1, 0, 0]]

How can I add the content from the matrix to the data frame in a new named column such that the data frame will end up like this:

[[1, 2, 3, [0, 1, 0]],

[4, 5, 6, [0, 0, 1]],

[7, 8, 9, [1, 0, 0]]]

Notice the data frame now has shape (X, Y+1) and rows from the matrix are elements in the data frame.

1 Answer

0 votes
by (41.4k points)

Using the below code:

import numpy as np

import pandas as pd

import scipy.sparse as sparse

df = pd.DataFrame(np.arange(1,10).reshape(3,3))

arr = sparse.coo_matrix(([1,1,1], ([0,1,2], [1,2,0])), shape=(3,3))

df['newcol'] = arr.toarray().tolist()

print(df)

Output:

    0  1  2     newcol

0  1  2  3  [0, 1, 0]

1  4  5  6  [0, 0, 1]

2  7  8  9  [1, 0, 0]

If you wish to know what is python visit this python tutorial and python interview questions.

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

30.5k questions

32.6k answers

500 comments

108k users

Browse Categories

...