问题:有没有一种快速的方法可以将2D Numpy矩阵转换为Pandas级数集?例如,一个(100 x5(ndarray,到5个系列,每个系列有100行。
背景:我需要使用随机生成的不同类型的数据(float、string等(创建pandas数据帧。目前,对于float,我创建了一个numpy矩阵,对于字符串,我创建一个字符串数组。然后,我将所有这些沿着轴=1组合起来,形成一个数据帧。这不会保留每个单独列的数据类型。
为了保留数据类型,我计划使用pandas系列。由于创建多个浮点数系列可能比创建浮点数的numpy矩阵慢,我想知道是否有办法将numpy矩阵转换为一组系列。
这个问题与我的不同之处在于,它询问的是如何将numpy矩阵转换为单个序列。我需要多个系列。
您可以将每个数据类型的矩阵直接转换为数据帧,然后将生成的数据帧连接起来。
float_df = pd.DataFrame(np.random.rand(500).reshape((-1,5)))
# 0 1 2 3 4
#0 0.561765 0.177957 0.279419 0.332973 0.967186
#1 0.761327 0.323747 0.707742 0.555475 0.680662
#.. ... ... ... ... ...
#98 0.741207 0.061200 0.142316 0.381168 0.591554
#99 0.417697 0.723469 0.730677 0.538261 0.281296
#
#[100 rows x 5 columns]
pd.concat([float_df, int_df, ...], axis=1)
从数组的dict生成数据帧:
In [571]: df = pd.DataFrame({'a':['one','two','three'], 'b':np.arange(3), 'c':np.ones(3)})
In [572]: df
Out[572]:
a b c
0 one 0 1.0
1 two 1 1.0
2 three 2 1.0
注意混合列数据类型:
In [579]: df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 a 3 non-null object
1 b 3 non-null int64
2 c 3 non-null float64
dtypes: float64(1), int64(1), object(1)
memory usage: 200.0+ bytes
如果我们从中要求一个numpy,我们会得到一个2d对象dtype数组:
In [580]: df.values
Out[580]:
array([['one', 0, 1.0],
['two', 1, 1.0],
['three', 2, 1.0]], dtype=object)
重新创建数据帧,看起来是一样的,但列的数据类型不同:
In [581]: pd.DataFrame(df.values, columns=['a','b','c'])
Out[581]:
a b c
0 one 0 1.0
1 two 1 1.0
2 three 2 1.0
In [582]: _.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 a 3 non-null object
1 b 3 non-null object
2 c 3 non-null object
dtypes: object(3)
memory usage: 200.0+ bytes
但是结构化数组确实保留了列dtpes:
In [587]: df.to_records(index=False)
Out[587]:
rec.array([('one', 0, 1.), ('two', 1, 1.), ('three', 2, 1.)],
dtype=[('a', 'O'), ('b', '<i8'), ('c', '<f8')])
In [588]: pd.DataFrame(_)
Out[588]:
a b c
0 one 0 1.0
1 two 1 1.0
2 three 2 1.0
In [589]: _.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 a 3 non-null object
1 b 3 non-null int64
2 c 3 non-null float64
dtypes: float64(1), int64(1), object(1)
memory usage: 200.0+ bytes