Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从数组中提取浮点数_Python_Arrays - Fatal编程技术网

Python 从数组中提取浮点数

Python 从数组中提取浮点数,python,arrays,Python,Arrays,我有一个数组: [(0, b'C', 5.8816, 6.0, 0.1184, 4.2631, 4.2631, 0.0) (1, b'H', 0.8495, 1.0, 0.1505, 0.951, 0.951, 0.0) (2, b'Br', 35.0064, 35.0, -0.0064, 1.2192, 1.2192, -0.0) (3, b'Cl', 17.0401, 17.0, -0.0401, 1.2405, 1.2405, -0.0) (4, b'F', 9.2225, 9.0

我有一个数组:

[(0, b'C', 5.8816, 6.0, 0.1184, 4.2631, 4.2631, 0.0)
 (1, b'H', 0.8495, 1.0, 0.1505, 0.951, 0.951, 0.0)
 (2, b'Br', 35.0064, 35.0, -0.0064, 1.2192, 1.2192, -0.0)
 (3, b'Cl', 17.0401, 17.0, -0.0401, 1.2405, 1.2405, -0.0)
 (4, b'F', 9.2225, 9.0, -0.2225, 1.0449, 1.0449, -0.0)]
我想从中提取单列浮点数,以便在程序中进一步使用

当我尝试以下方法时:

QA = []
idx_IN_columns = [5]
QA = data[idx_IN_columns]
我得到:

Traceback (most recent call last):

  File "<ipython-input-22-4e6a1b6a3f36>", line 1, in <module>
    runfile('C:/Users/Steve/Python/Testing/ReadFile_mpa_1.py', wdir='C:/Users/Steve/Python/Testing')

  File "C:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)

  File "C:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)
    enter code here

  File "C:/Users/Steve/Python/Testing/ReadFile_mpa_1.py", line 34, in <module>
    QA = data[idx_IN_columns]

IndexError: index 5 is out of bounds for axis 1 with size 5
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
runfile('C:/Users/Steve/Python/Testing/ReadFile\u mpa\u 1.py',wdir='C:/Users/Steve/Python/Testing')
文件“C:\Anaconda3\lib\site packages\spyder\utils\site\sitecustomize.py”,第866行,在runfile中
execfile(文件名、命名空间)
文件“C:\Anaconda3\lib\site packages\spyder\utils\site\sitecustomize.py”,第102行,在execfile中
exec(编译(f.read(),文件名,'exec'),命名空间)
在这里输入代码
文件“C:/Users/Steve/Python/Testing/ReadFile_mpa_1.py”,第34行,在
QA=数据[idx_在_列中]
索引器:索引5超出大小为5的轴1的界限
我们将不胜感激

提前谢谢

data = [(0, b'C', 5.8816, 6.0, 0.1184, 4.2631, 4.2631, 0.0),
 (1, b'H', 0.8495, 1.0, 0.1505, 0.951, 0.951, 0.0),
 (2, b'Br', 35.0064, 35.0, -0.0064, 1.2192, 1.2192, -0.0),
 (3, b'Cl', 17.0401, 17.0, -0.0401, 1.2405, 1.2405, -0.0),
 (4, b'F', 9.2225, 9.0, -0.2225, 1.0449, 1.0449, -0.0)]

numbers = [n[2:] for n in data]
后果
不能使用列表作为索引访问列表元素(请参阅),需要迭代。您的数据集是元组;它们类似于列表,但不能修改。 顺便说一下,您的数据格式不正确(缺少逗号):


更高级一点,如:


假设数组的末尾有逗号,如

data = [(0, b'C', 5.8816, 6.0, 0.1184, 4.2631, 4.2631, 0.0),
 (1, b'H', 0.8495, 1.0, 0.1505, 0.951, 0.951, 0.0),
 (2, b'Br', 35.0064, 35.0, -0.0064, 1.2192, 1.2192, -0.0),
 (3, b'Cl', 17.0401, 17.0, -0.0401, 1.2405, 1.2405, -0.0),
 (4, b'F', 9.2225, 9.0, -0.2225, 1.0449, 1.0449, -0.0)]
这实际上是一个元组列表(不是真正的数组),您可以

QA = [row[5] for row in data]


您的数据以及您试图使用它所做的一切听起来非常适合于一个以表格格式处理数据的库

>>> import pandas as pd
>>> data = [(0, b'C', 5.8816, 6.0, 0.1184, 4.2631, 4.2631, 0.0),
 (1, b'H', 0.8495, 1.0, 0.1505, 0.951, 0.951, 0.0),
 (2, b'Br', 35.0064, 35.0, -0.0064, 1.2192, 1.2192, -0.0),
 (3, b'Cl', 17.0401, 17.0, -0.0401, 1.2405, 1.2405, -0.0),
 (4, b'F', 9.2225, 9.0, -0.2225, 1.0449, 1.0449, -0.0)]
>>> df = pd.DataFrame(data)
>>> df
   0      1        2     3       4       5       6    7
0  0   b'C'   5.8816   6.0  0.1184  4.2631  4.2631  0.0
1  1   b'H'   0.8495   1.0  0.1505  0.9510  0.9510  0.0
2  2  b'Br'  35.0064  35.0 -0.0064  1.2192  1.2192 -0.0
3  3  b'Cl'  17.0401  17.0 -0.0401  1.2405  1.2405 -0.0
4  4   b'F'   9.2225   9.0 -0.2225  1.0449  1.0449 -0.0
然后,要获得第5列,只需执行
df[5]

>>> df[5]
0    4.2631
1    0.9510
2    1.2192
3    1.2405
4    1.0449
Name: 5, dtype: float64

例如,通过使用Pandas,您还可以轻松地从(
pd.read\u csv()
)读取数据,并将数据(
df.to\u csv()
)写入磁盘。

您是否试图从数组中获取索引为5的元素?数组令人困惑,它是一个只有1d数组的二维数组,其元素是元组。你误用了逗号吗?@Steve注意到你的全名显示在你发布的错误消息的路径中。@Steve我编辑了它,但它仍在历史记录中。我不得不对你的解决方案做一点修改。
QA = [row[5] for row in data]
from operator import itemgetter

QA = map(itemgetter(5), data)
>>> import pandas as pd
>>> data = [(0, b'C', 5.8816, 6.0, 0.1184, 4.2631, 4.2631, 0.0),
 (1, b'H', 0.8495, 1.0, 0.1505, 0.951, 0.951, 0.0),
 (2, b'Br', 35.0064, 35.0, -0.0064, 1.2192, 1.2192, -0.0),
 (3, b'Cl', 17.0401, 17.0, -0.0401, 1.2405, 1.2405, -0.0),
 (4, b'F', 9.2225, 9.0, -0.2225, 1.0449, 1.0449, -0.0)]
>>> df = pd.DataFrame(data)
>>> df
   0      1        2     3       4       5       6    7
0  0   b'C'   5.8816   6.0  0.1184  4.2631  4.2631  0.0
1  1   b'H'   0.8495   1.0  0.1505  0.9510  0.9510  0.0
2  2  b'Br'  35.0064  35.0 -0.0064  1.2192  1.2192 -0.0
3  3  b'Cl'  17.0401  17.0 -0.0401  1.2405  1.2405 -0.0
4  4   b'F'   9.2225   9.0 -0.2225  1.0449  1.0449 -0.0
>>> df[5]
0    4.2631
1    0.9510
2    1.2192
3    1.2405
4    1.0449
Name: 5, dtype: float64