将元组数组转换为二维数组 [英] convert array of tuples to 2 dimensional array

查看:108
本文介绍了将元组数组转换为二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 np.genfromtxt()函数从csv文件加载的元组数组.

I have an array of tuples loaded from a csv file using np.genfromtxt() function.

import numpy as np
import re
from matplotlib.dates import strpdate2num
def convert_string_to_bigint(x):
    p = re.compile(r'(\d{4})/(\d{1,2})/(\d{1,2}) (\d{1,2}):(\d{2}):\d{2}')
    m = p.findall(x)
    l = list(m[0])
    l[1] = ('0' + l[1])[-2:]
    l[2] = ('0' + l[2])[-2:]
    return long("".join(l))

#print convert_string_to_bigint("2012/7/2 14:07:00")
csv = np.genfromtxt ('sr00-1min.txt', delimiter=',', converters={0:convert_string_to_bigint})

csv文件中的数据样本:

The data sample in the csv file:

2015/9/2 14:54:00,5169,5170,5167,5168
2015/9/2 14:55:00,5168,5169,5166,5166
2015/9/2 14:56:00,5167,5170,5165,5169
2015/9/2 14:57:00,5168,5173,5167,5172
2015/9/2 14:58:00,5172,5187,5171,5182
2015/9/2 14:59:00,5182,5183,5171,5176
2015/9/2 15:00:00,5176,5183,5174,5182

加载后,看起来像这样:

After it is loaded, it looked like this:

[(201509021455L, 5168.0, 5169.0, 5166.0, 5166.0)
 (201509021456L, 5167.0, 5170.0, 5165.0, 5169.0)
 (201509021457L, 5168.0, 5173.0, 5167.0, 5172.0)
 (201509021458L, 5172.0, 5187.0, 5171.0, 5182.0)
 (201509021459L, 5182.0, 5183.0, 5171.0, 5176.0)
 (201509021500L, 5176.0, 5183.0, 5174.0, 5182.0)]

我想将其转换为一个numpy的2d数组.它应该像这样:

And I want to convert it to a numpy 2d array. It should like this:

[[201509021455L, 5168.0, 5169.0, 5166.0, 5166.0]
 [201509021456L, 5167.0, 5170.0, 5165.0, 5169.0]
 [201509021457L, 5168.0, 5173.0, 5167.0, 5172.0]
 [201509021458L, 5172.0, 5187.0, 5171.0, 5182.0]
 [201509021459L, 5182.0, 5183.0, 5171.0, 5176.0]
 [201509021500L, 5176.0, 5183.0, 5174.0, 5182.0]]

我用下面的代码解决了这个问题,但是看起来很难看.有人可以告诉我如何以一种优雅的方式转换它吗?

I used code below to solve the question, but it looks extreamly ugly.Could anyone tell me how to convert it in an elegant way?

pool = np.asarray([x for x in csv if x[0] > 201508010000])
sj = np.asarray([x[0] for x in pool])
kpj = np.asarray([x[1] for x in pool])
zgj = np.asarray([x[2] for x in pool])
zdj = np.asarray([x[3] for x in pool])
spj = np.asarray([x[4] for x in pool])
output = np.column_stack((sj,kpj,zgj,zdj,spj))
print output.shape

推荐答案

convert_string_to_bigint 中,更改

return long("".join(l))

return float("".join(l))

然后 genfromtxt 将所有值识别为float,并返回float dtype的2D数组:

Then genfromtxt will recognize all values as floats, and return a 2D array of float dtype:

In [23]: np.genfromtxt ('sr00-1min.txt', delimiter=',', converters={0:convert_string_to_bigint}).shape
Out[23]: (7, 5)

代替混合dtype的一维结构化数组.

instead of a 1D structured array of mixed dtype.

这篇关于将元组数组转换为二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆