如何在Python中使用列表和数组创建结构化数组?
我有一个表格列表:如何在Python中使用列表和数组创建结构化数组?,python,arrays,numpy,scipy,Python,Arrays,Numpy,Scipy,我有一个表格列表: A = ['P', 'Q', 'R', 'S', 'T', 'U'] B = [[ 1 2 3 4 5 6] [ 7 8 9 10 11 12] [13 14 15 16 17 18] [19 20 21 22 23 24]] C = [[ P Q R S T U] [ 1 2 3 4 5 6] [ 7 8 9 10 11 12] [13 14 15 16 17 18]
A = ['P', 'Q', 'R', 'S', 'T', 'U']
B = [[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]
[13 14 15 16 17 18]
[19 20 21 22 23 24]]
C = [[ P Q R S T U]
[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]
[13 14 15 16 17 18]
[19 20 21 22 23 24]]
以及以下形式的数组B:
A = ['P', 'Q', 'R', 'S', 'T', 'U']
B = [[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]
[13 14 15 16 17 18]
[19 20 21 22 23 24]]
C = [[ P Q R S T U]
[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]
[13 14 15 16 17 18]
[19 20 21 22 23 24]]
现在我想创建一个结构化数组C,其形式如下:
A = ['P', 'Q', 'R', 'S', 'T', 'U']
B = [[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]
[13 14 15 16 17 18]
[19 20 21 22 23 24]]
C = [[ P Q R S T U]
[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]
[13 14 15 16 17 18]
[19 20 21 22 23 24]]
因此,我可以提取列名为p、Q、R等的列。我尝试了以下代码,但它没有创建结构化数组,并给出以下错误
代码
import numpy as np
A = (['P', 'Q', 'R', 'S', 'T', 'U'])
B = np.array([[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24]])
C = np.vstack((A, B))
print (C)
D = C['P']
错误
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
在这种情况下,如何在Python中创建结构化数组
更新
import numpy as np
A = (['P', 'Q', 'R', 'S', 'T', 'U'])
B = np.array([[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24]])
C = np.vstack((A, B))
print (C)
D = C['P']
这两个都是变量,它们的形状在运行时会发生变化,但列表和数组的列数相同。这就是库的用途:
>>> A = ['P', 'Q', 'R', 'S', 'T', 'U']
>>> B = np.arange(1, 25).reshape(4, 6)
>>> B
array([[ 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18],
[19, 20, 21, 22, 23, 24]])
>>> import pandas as pd
>>> pd.DataFrame(B, columns=A)
P Q R S T U
0 1 2 3 4 5 6
1 7 8 9 10 11 12
2 13 14 15 16 17 18
3 19 20 21 22 23 24
>>> df = pd.DataFrame(B, columns=A)
>>> df['P']
0 1
1 7
2 13
3 19
Name: P, dtype: int64
>>> df['T']
0 5
1 11
2 17
3 23
Name: T, dtype: int64
>>>
>>> A = ['P', 'Q', 'R', 'S', 'T', 'U']
>>> B = np.arange(1, 25).reshape(4, 6)
>>> B
array([[ 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18],
[19, 20, 21, 22, 23, 24]])
>>> import pandas as pd
>>> pd.DataFrame(B, columns=A)
P Q R S T U
0 1 2 3 4 5 6
1 7 8 9 10 11 12
2 13 14 15 16 17 18
3 19 20 21 22 23 24
>>> df = pd.DataFrame(B, columns=A)
>>> df['P']
0 1
1 7
2 13
3 19
Name: P, dtype: int64
>>> df['T']
0 5
1 11
2 17
3 23
Name: T, dtype: int64
>>>
>>> A = ['P', 'Q', 'R', 'S', 'T', 'U']
>>> B = np.arange(1, 25).reshape(4, 6)
>>> B
array([[ 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18],
[19, 20, 21, 22, 23, 24]])
>>> import pandas as pd
>>> pd.DataFrame(B, columns=A)
P Q R S T U
0 1 2 3 4 5 6
1 7 8 9 10 11 12
2 13 14 15 16 17 18
3 19 20 21 22 23 24
>>> df = pd.DataFrame(B, columns=A)
>>> df['P']
0 1
1 7
2 13
3 19
Name: P, dtype: int64
>>> df['T']
0 5
1 11
2 17
3 23
Name: T, dtype: int64
>>>
>>> A = ['P', 'Q', 'R', 'S', 'T', 'U']
>>> B = np.arange(1, 25).reshape(4, 6)
>>> B
array([[ 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18],
[19, 20, 21, 22, 23, 24]])
>>> import pandas as pd
>>> pd.DataFrame(B, columns=A)
P Q R S T U
0 1 2 3 4 5 6
1 7 8 9 10 11 12
2 13 14 15 16 17 18
3 19 20 21 22 23 24
>>> df = pd.DataFrame(B, columns=A)
>>> df['P']
0 1
1 7
2 13
3 19
Name: P, dtype: int64
>>> df['T']
0 5
1 11
2 17
3 23
Name: T, dtype: int64
>>>
numpy
做这件事,你可以
A = np.array(['P', 'Q', 'R', 'S', 'T', 'U'])
B = np.array([[ 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18],
[19, 20, 21, 22, 23, 24]])
# define the structured array with the names from A
C = np.zeros(B.shape[0],dtype={'names':A,'formats':['f8','f8','f8','f8','f8','f8']})
# copy the data from B into C
for i,n in enumerate(A):
C[n] = B[:,i]
C['Q']
array([ 2., 8., 14., 20.])
编辑:您可以改为使用自动化格式列表
C = np.zeros(B.shape[0],dtype={'names':A,'formats':['f8' for x in range(A.shape[0])]})
此外,名称不会作为数据出现在C
中,而是出现在dtype
中。要从C
获取名称,您可以使用
C.dtype.names
如果你想用纯
numpy
来做,你可以这样做
A = np.array(['P', 'Q', 'R', 'S', 'T', 'U'])
B = np.array([[ 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18],
[19, 20, 21, 22, 23, 24]])
# define the structured array with the names from A
C = np.zeros(B.shape[0],dtype={'names':A,'formats':['f8','f8','f8','f8','f8','f8']})
# copy the data from B into C
for i,n in enumerate(A):
C[n] = B[:,i]
C['Q']
array([ 2., 8., 14., 20.])
编辑:您可以改为使用自动化格式列表
C = np.zeros(B.shape[0],dtype={'names':A,'formats':['f8' for x in range(A.shape[0])]})
此外,名称不会作为数据出现在C
中,而是出现在dtype
中。要从C
获取名称,您可以使用
C.dtype.names
如果你想用纯
numpy
来做,你可以这样做
A = np.array(['P', 'Q', 'R', 'S', 'T', 'U'])
B = np.array([[ 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18],
[19, 20, 21, 22, 23, 24]])
# define the structured array with the names from A
C = np.zeros(B.shape[0],dtype={'names':A,'formats':['f8','f8','f8','f8','f8','f8']})
# copy the data from B into C
for i,n in enumerate(A):
C[n] = B[:,i]
C['Q']
array([ 2., 8., 14., 20.])
编辑:您可以改为使用自动化格式列表
C = np.zeros(B.shape[0],dtype={'names':A,'formats':['f8' for x in range(A.shape[0])]})
此外,名称不会作为数据出现在C
中,而是出现在dtype
中。要从C
获取名称,您可以使用
C.dtype.names
如果你想用纯
numpy
来做,你可以这样做
A = np.array(['P', 'Q', 'R', 'S', 'T', 'U'])
B = np.array([[ 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18],
[19, 20, 21, 22, 23, 24]])
# define the structured array with the names from A
C = np.zeros(B.shape[0],dtype={'names':A,'formats':['f8','f8','f8','f8','f8','f8']})
# copy the data from B into C
for i,n in enumerate(A):
C[n] = B[:,i]
C['Q']
array([ 2., 8., 14., 20.])
编辑:您可以改为使用自动化格式列表
C = np.zeros(B.shape[0],dtype={'names':A,'formats':['f8' for x in range(A.shape[0])]})
此外,名称不会作为数据出现在C
中,而是出现在dtype
中。要从C
获取名称,您可以使用
C.dtype.names
您的错误发生在:
D = C['P']
下面是一个简单的方法,在标题行上使用常规Python列表
import numpy as np
A = (['P', 'Q', 'R', 'S', 'T', 'U'])
B = np.array([[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24]])
C = np.vstack((A, B))
print (C)
D = C[0:len(C), list(C[0]).index('P')]
print (D)
您的错误发生在:
D = C['P']
下面是一个简单的方法,在标题行上使用常规Python列表
import numpy as np
A = (['P', 'Q', 'R', 'S', 'T', 'U'])
B = np.array([[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24]])
C = np.vstack((A, B))
print (C)
D = C[0:len(C), list(C[0]).index('P')]
print (D)
您的错误发生在:
D = C['P']
下面是一个简单的方法,在标题行上使用常规Python列表
import numpy as np
A = (['P', 'Q', 'R', 'S', 'T', 'U'])
B = np.array([[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24]])
C = np.vstack((A, B))
print (C)
D = C[0:len(C), list(C[0]).index('P')]
print (D)
您的错误发生在:
D = C['P']
下面是一个简单的方法,在标题行上使用常规Python列表
import numpy as np
A = (['P', 'Q', 'R', 'S', 'T', 'U'])
B = np.array([[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24]])
C = np.vstack((A, B))
print (C)
D = C[0:len(C), list(C[0]).index('P')]
print (D)
B
中是否缺少一些列?我已更新了数组B。您对更改的意思是什么?增加。。。减少至少numpy
“更喜欢”具有固定的形状,这就是为什么您应该首先获取所有数据,然后启动numpy机器。我的意思是列的数量可能会根据数据集而增加或减少。您的C
看起来不像结构化数组(由genfromtxt
生成)。充其量它是一个同时包含字符串和数字的dtype对象数组,或者是一个字符串数组,其中的引号已被编辑掉。B
中是否缺少某些列?我已更新了数组B。更改意味着什么?增加。。。减少至少numpy
“更喜欢”具有固定的形状,这就是为什么您应该首先获取所有数据,然后启动numpy机器。我的意思是列的数量可能会根据数据集而增加或减少。您的C
看起来不像结构化数组(由genfromtxt
生成)。充其量它是一个同时包含字符串和数字的dtype对象数组,或者是一个字符串数组,其中的引号已被编辑掉。B
中是否缺少某些列?我已更新了数组B。更改意味着什么?增加。。。减少至少numpy
“更喜欢”具有固定的形状,这就是为什么您应该首先获取所有数据,然后启动numpy机器。我的意思是列的数量可能会根据数据集而增加或减少。您的C
看起来不像结构化数组(由genfromtxt
生成)。充其量它是一个同时包含字符串和数字的dtype对象数组,或者是一个字符串数组,其中的引号已被编辑掉。B
中是否缺少某些列?我已更新了数组B。更改意味着什么?增加。。。减少至少numpy
“更喜欢”具有固定的形状,这就是为什么您应该首先获取所有数据,然后启动numpy机器。我的意思是列的数量可能会根据数据集而增加或减少。您的C
看起来不像结构化数组(由genfromtxt
生成)。充其量是一个包含字符串和数字的dtype对象数组,或者是一个字符串数组,其中引号已被编辑掉。我是否必须为每列添加格式,对于大量列,还有其他解决方法吗?@nxcr:我认为您需要格式部分,但您可以将其自动化->查看我的编辑C中缺少标题。如何将标题(即数组A)保留在数组C中?@nxcr:因为名称不是数据的一部分。有关名称列表,请键入C.dtype.names
。当您只需键入C
时,也可以在dtype
中看到它们。当你想拥有类似Excel“视图”的东西时,你必须使用pandas
代替,但是当我使用np.genfromtxt()生成数组时,我会得到这样的标题。我必须为每列添加格式吗,对于大量列,还有其他解决方法吗?@nxcr:我认为您需要格式部分,但您可以将其自动化->查看我的编辑C中缺少标题。如何将标题(即数组A)保留在数组C中?@nxcr:因为名称不是数据的一部分。有关名称列表,请键入C.dtype.names
。当您只需键入C
时,也可以在dtype
中看到它们。当你想要像Excel“视图”这样的东西时,你必须使用pandas
而不是head,但是当我使用np.genfromtxt()生成数组时,我会得到这样的头。我必须添加吗