Reputation: 2785
We have a dataframe where the elements of one column are lists (the discussion is not about if this should be done or not). A simple example is the following:
df = pd.DataFrame([[12,[123,234,234]], [14,[124,25,235]], [16,[1267,267,2345]]], columns = ['A', 'B'])
obtaining:
the goal here to to convert the column B into a numpy array, like the following one:
If I ask to pandas convert the column into an array:
df['B'].values
it returns an array of list, which is not the same as the one above:
array([list([123, 234, 234]), list([124, 25, 235]),
list([1267, 267, 2345])], dtype=object)
How can we solve the problem?
Upvotes: 3
Views: 3421
Reputation: 862511
If always same length of lists is possible create nested lists and then convert to np.array
:
arr = np.array(df['B'].values.tolist())
#alternative
#arr = np.array(df['B'].tolist())
print (arr)
[[ 123 234 234]
[ 124 25 235]
[1267 267 2345]]
Upvotes: 2