简体   繁体   中英

conversion of lists into a numpy array in pandas dataframe

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?

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

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