1

The df below contains integers grouped by time. I’m trying to convert these to a numpy array.

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'Time' : [1,1,1,2,2,2,3,3,3],        
    'A' : [3, 4, 5, 2, 5, 6, 1, 6, 7], 
    'B' : [2, 4, 5, 2, 5, 5, 2, 6, 5],                           
})

GA = np.array(df.groupby(['Time'])['A'].apply(np.array))

Intended Out:

[[ 3  4  5]
 [ 2  5  6]
 [ 1  6  7]]

1 Answer 1

2

use to to list before converting

np.array(df.groupby('Time')['A'].apply(list).tolist())

Out:

array([[3, 4, 5],
   [2, 5, 6],
   [1, 6, 7]])
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.