4

Consider a dataframe like:

df = pd.DataFrame({'r': [1, 1, 2, 2], 'c': [0, 2, 1, 2], 'v': [2, 4, 3, 5],})

I want to extract a numpy array or tensor considering 'r' and 'c' as row and column index of the matrix. so the corresponding matrix will be like:

arr = array([[0, 0, 0],
   [2, 0, 4],
   [0, 3, 5]])

So is there a decent way to do that or I have to loop through each df row? what about extracting dataframe df from a matrix like arr?

1 Answer 1

6

To create a numpy array from the dataframe considering r and c as the row and column index:

r, c, v = df.T.values
arr = np.zeros((r.max() + 1, c.max() + 1))
arr[r, c] = v

>>> arr

array([[0., 0., 0.],
       [2., 0., 4.],
       [0., 3., 5.]])

To recreate the dataframe from the numpy array like above you can use np.nonzero to get the indices of the elements that are non-zero:

r, c = np.nonzero(arr)
df = pd.DataFrame({'r': r, 'c': c, 'v': arr[r, c]})

>>> df

   r  c    v
0  1  0  2.0
1  1  2  4.0
2  2  1  3.0
3  2  2  5.0
Sign up to request clarification or add additional context in comments.

Comments

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.