简体   繁体   English

将列表转换为 Pandas 数据框中的 numpy 数组

[英]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:这里的目标是将B列转换为一个 numpy 数组,如下所示:

在此处输入图片说明 . .

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 :如果列表的长度总是相同,则可以创建嵌套列表,然后转换为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]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM