大家好我有一个2d数组,我想创建一个只有2,3列的新2d数组
这是我的代码
#!/user
# -*- coding: utf-8 -*-
import csv
import urllib2
import numpy as np
url = 'https://api.bmreports.com/BMRS/FUELINST/v1?APIKey=66ky5jo5p5w0vbd&ServiceType=CSV'
url2 = 'https://api.bmreports.com/BMRS/FUELINSTHHCUR/v1?APIKey=66ky5jo5p5w0vbd&ServiceType=CSV'
response = urllib2.urlopen(url2)
cr = csv.reader(response)
arr = np.genfromtxt(response,delimiter=",", skip_header=1, skip_footer=2,dtype=None)
data = arr[:, [1:2]]
print data
所以数据会像这样回来
[('FUELINSTHHCUR', 'CCGT', 10430, 35.8, 10282, 35.2, 205996, 32. )
('FUELINSTHHCUR', 'OCGT', 0, 0. , 0, 0. , 17, 0. )
('FUELINSTHHCUR', 'OIL', 0, 0. , 0, 0. , 0, 0. )
('FUELINSTHHCUR', 'COAL', 0, 0. , 0, 0. , 0, 0. )
('FUELINSTHHCUR', 'NUCLEAR', 6963, 23.9, 6970, 23.9, 167591, 26. )
('FUELINSTHHCUR', 'WIND', 6986, 24. , 7061, 24.2, 160036, 24.9)
('FUELINSTHHCUR', 'PS', 297, 1. , 412, 1.4, 8136, 1.3)
('FUELINSTHHCUR', 'NPSHYD', 322, 1.1, 319, 1.1, 8015, 1.2)
('FUELINSTHHCUR', 'OTHER', 129, 0.4, 128, 0.4, 3093, 0.5)
('FUELINSTHHCUR', 'INTFR', 1494, 5.1, 1494, 5.1, 31731, 4.9)
('FUELINSTHHCUR', 'INTIRL', 0, 0. , 0, 0. , 2650, 0.4)
('FUELINSTHHCUR', 'INTNED', 882, 3. , 880, 3. , 18991, 2.9)
('FUELINSTHHCUR', 'INTEW', 0, 0. , 0, 0. , 0, 0. )
('FUELINSTHHCUR', 'BIOMASS', 1608, 5.5, 1630, 5.6, 37688, 5.9)]
我正在尝试创建一个新的2d数组,只返回列,所以它看起来像这样
[('CCGT', 10430)
('OCGT', 0)
('OIL', 0)
('COAL', 0)
('NUCLEAR', 6963)
('WIND', 6986)
('PS', 297)
('NPSHYD', 322)
('OTHER', 129)
('INTFR', 1494)
('INTIRL', 0)
('INTNED', 882)
('INTEW', 0)
('BIOMASS', 1608)]
答案 0 :(得分:1)
首先,你需要使用Numpy数组来保存你的项而不是列表,然后你可以将列表中的列索引传递给第二个轴以获得所需的结果:
data = arr[:, [2, 3]]
或切片如下:
data = arr[:, 2:4]
此外,如果您正在阅读的字符串格式正确,而不是使用csv
你可以使用fromstring()
用于加载数据的功能。
答案 1 :(得分:0)
如果您查看此代码示例,它可能会对您有所帮助。
它假设您收集的第一个值是numpy数组中的单个字符串....因此每行包含逗号和引用元素,但它们都嵌入在单个元素中。
var'lst'是一个列表,用于遍历所有数组元素并创建包含逗号分隔元素作为单个字符串的多维列表列表。
var'tmp'是一个由新解析的'lst'构成的numpy数组。
var'data'是来自'tmp'的切片numpy数组,只包含您要查找的列。
希望有帮助...
import numpy as np def ElementSplit(x) : return x.split(",") ElementSplit = np.vectorize(ElementSplit) arr = np.array([["'FUELINSTHHCUR', 'CCGT', 10430, 35.8, 10282, 35.2, 205996, 32. "], ["'FUELINSTHHCUR', 'OCGT', 0, 0. , 0, 0. , 17, 0. "], [" 'FUELINSTHHCUR', 'OIL', 0, 0. , 0, 0. , 0, 0. "], [" 'FUELINSTHHCUR', 'COAL', 0, 0. , 0, 0. , 0, 0. "], [" 'FUELINSTHHCUR', 'NUCLEAR', 6963, 23.9, 6970, 23.9, 167591, 26. "], [" 'FUELINSTHHCUR', 'WIND', 6986, 24. , 7061, 24.2, 160036, 24.9"], [" 'FUELINSTHHCUR', 'PS', 297, 1. , 412, 1.4, 8136, 1.3"], [" 'FUELINSTHHCUR', 'NPSHYD', 322, 1.1, 319, 1.1, 8015, 1.2"], [" 'FUELINSTHHCUR', 'OTHER', 129, 0.4, 128, 0.4, 3093, 0.5"], [" 'FUELINSTHHCUR', 'INTFR', 1494, 5.1, 1494, 5.1, 31731, 4.9"], [" 'FUELINSTHHCUR', 'INTIRL', 0, 0. , 0, 0. , 2650, 0.4"], [" 'FUELINSTHHCUR', 'INTNED', 882, 3. , 880, 3. , 18991, 2.9"], [" 'FUELINSTHHCUR', 'INTEW', 0, 0. , 0, 0. , 0, 0. "], [" 'FUELINSTHHCUR', 'BIOMASS', 1608, 5.5, 1630, 5.6, 37688, 5.9"]]) print " --arr--" print arr lst = [] for items in arr: lst.append(items[0].split(", ")) print " --lst--" print lst exit #data = arr[:] tmp = np.array(lst) print " --tmp--" print tmp data = tmp[:,[1,2]] print " --data--" print data
,输出看起来像这样......
$python main.py --arr-- [["'FUELINSTHHCUR', 'CCGT', 10430, 35.8, 10282, 35.2, 205996, 32. "] ["'FUELINSTHHCUR', 'OCGT', 0, 0. , 0, 0. , 17, 0. "] [" 'FUELINSTHHCUR', 'OIL', 0, 0. , 0, 0. , 0, 0. "] [" 'FUELINSTHHCUR', 'COAL', 0, 0. , 0, 0. , 0, 0. "] [" 'FUELINSTHHCUR', 'NUCLEAR', 6963, 23.9, 6970, 23.9, 167591, 26. "] [" 'FUELINSTHHCUR', 'WIND', 6986, 24. , 7061, 24.2, 160036, 24.9"] [" 'FUELINSTHHCUR', 'PS', 297, 1. , 412, 1.4, 8136, 1.3"] [" 'FUELINSTHHCUR', 'NPSHYD', 322, 1.1, 319, 1.1, 8015, 1.2"] [" 'FUELINSTHHCUR', 'OTHER', 129, 0.4, 128, 0.4, 3093, 0.5"] [" 'FUELINSTHHCUR', 'INTFR', 1494, 5.1, 1494, 5.1, 31731, 4.9"] [" 'FUELINSTHHCUR', 'INTIRL', 0, 0. , 0, 0. , 2650, 0.4"] [" 'FUELINSTHHCUR', 'INTNED', 882, 3. , 880, 3. , 18991, 2.9"] [" 'FUELINSTHHCUR', 'INTEW', 0, 0. , 0, 0. , 0, 0. "] [" 'FUELINSTHHCUR', 'BIOMASS', 1608, 5.5, 1630, 5.6, 37688, 5.9"]] --lst-- [["'FUELINSTHHCUR'", "'CCGT'", '10430', '35.8', '10282', '35.2', '205996', '32. '], ["'FUELINSTHHCUR'", "'OCGT'", ' 0', ' 0. ', ' 0', ' 0. ', ' 17', ' 0. '], [" 'FUELINSTHHCUR'", "'OIL'", ' 0', ' 0. ', ' 0', ' 0. ', ' 0', ' 0. '], [" 'FUELINSTHHCUR'", "'COAL'", ' 0', ' 0. ', ' 0', ' 0. ', ' 0', ' 0. '], [" 'FUELINSTHHCUR'", "'NUCLEAR'", ' 6963', '23.9', ' 6970', '23.9', '167591', '26. '], [" 'FUELINSTHHCUR'", "'WIND'", ' 6986', '24. ', ' 7061', '24.2', '160036', '24.9'], [" 'FUELINSTHHCUR'", "'PS'", ' 297', ' 1. ', ' 412', ' 1.4', ' 8136', ' 1.3'], [" 'FUELINSTHHCUR'", "'NPSHYD'", ' 322', ' 1.1', ' 319', ' 1.1', ' 8015', ' 1.2'], [" 'FUELINSTHHCUR'", "'OTHER'", ' 129', ' 0.4', ' 128', ' 0.4', ' 3093', ' 0.5'], [" 'FUELINSTHHCUR'", "'INTFR'", ' 1494', ' 5.1', ' 1494', ' 5.1', ' 31731', ' 4.9'], [" 'FUELINSTHHCUR'", "'INTIRL'", ' 0', ' 0. ', ' 0', ' 0. ', ' 2650', ' 0.4'], [" 'FUELINSTHHCUR'", "'INTNED'", ' 882', ' 3. ', ' 880', ' 3. ', ' 18991', ' 2.9'], [" 'FUELINSTHHCUR'", "'INTEW'", ' 0', ' 0. ', ' 0', ' 0. ', ' 0', ' 0. '], [" 'FUELINSTHHCUR'", "'BIOMASS'", ' 1608', ' 5.5', ' 1630', ' 5.6', ' 37688', ' 5.9']] --tmp-- [["'FUELINSTHHCUR'" "'CCGT'" '10430' '35.8' '10282' '35.2' '205996' '32. '] ["'FUELINSTHHCUR'" "'OCGT'" ' 0' ' 0. ' ' 0' ' 0. ' ' 17' ' 0. '] [" 'FUELINSTHHCUR'" "'OIL'" ' 0' ' 0. ' ' 0' ' 0. ' ' 0' ' 0. '] [" 'FUELINSTHHCUR'" "'COAL'" ' 0' ' 0. ' ' 0' ' 0. ' ' 0' ' 0. '] [" 'FUELINSTHHCUR'" "'NUCLEAR'" ' 6963' '23.9' ' 6970' '23.9' '167591' '26. '] [" 'FUELINSTHHCUR'" "'WIND'" ' 6986' '24. ' ' 7061' '24.2' '160036' '24.9'] [" 'FUELINSTHHCUR'" "'PS'" ' 297' ' 1. ' ' 412' ' 1.4' ' 8136' ' 1.3'] [" 'FUELINSTHHCUR'" "'NPSHYD'" ' 322' ' 1.1' ' 319' ' 1.1' ' 8015' ' 1.2'] [" 'FUELINSTHHCUR'" "'OTHER'" ' 129' ' 0.4' ' 128' ' 0.4' ' 3093' ' 0.5'] [" 'FUELINSTHHCUR'" "'INTFR'" ' 1494' ' 5.1' ' 1494' ' 5.1' ' 31731' ' 4.9'] [" 'FUELINSTHHCUR'" "'INTIRL'" ' 0' ' 0. ' ' 0' ' 0. ' ' 2650' ' 0.4'] [" 'FUELINSTHHCUR'" "'INTNED'" ' 882' ' 3. ' ' 880' ' 3. ' ' 18991' ' 2.9'] [" 'FUELINSTHHCUR'" "'INTEW'" ' 0' ' 0. ' ' 0' ' 0. ' ' 0' ' 0. '] [" 'FUELINSTHHCUR'" "'BIOMASS'" ' 1608' ' 5.5' ' 1630' ' 5.6' ' 37688' ' 5.9']] --data-- [["'CCGT'" '10430'] ["'OCGT'" ' 0'] ["'OIL'" ' 0'] ["'COAL'" ' 0'] ["'NUCLEAR'" ' 6963'] ["'WIND'" ' 6986'] ["'PS'" ' 297'] ["'NPSHYD'" ' 322'] ["'OTHER'" ' 129'] ["'INTFR'" ' 1494'] ["'INTIRL'" ' 0'] ["'INTNED'" ' 882'] ["'INTEW'" ' 0'] ["'BIOMASS'" ' 1608']]