转换结构阵列各种数值类型以规则排列 [英] Convert structured array with various numeric data types to regular array
本文介绍了转换结构阵列各种数值类型以规则排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有各种数值数据类型的一个numpy的结构数组。作为一个基本的例子,
Suppose I have a NumPy structured array with various numeric datatypes. As a basic example,
my_data = np.array( [(17, 182.1), (19, 175.6)], dtype='i2,f4')
我怎样才能投进去花车的常规numpy的阵列呢?
How can I cast this into a regular NumPy array of floats?
From this answer, I know I could use
np.array(my_data.tolist())
但显然它是缓慢的,因为你转换一个有效的填充numpy的阵列到正规Python列表。
but apparently it is slow since you "convert an efficiently packed NumPy array to a regular Python list".
推荐答案
您可以轻松地与大熊猫做到这一点:
You can do it easily with Pandas:
>>> import pandas as pd
>>> pd.DataFrame(my_data).values
array([[ 17. , 182.1000061],
[ 19. , 175.6000061]], dtype=float32)
这篇关于转换结构阵列各种数值类型以规则排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文