[英]Convert Pandas Series of 2D numpy arrays to Pandas DataFrame of columns of 1D numpy arrays
First post to stackoverflow. 第一篇文章到stackoverflow。 I have searched an cannot find an answer to this.
我搜索过一个找不到答案。
I have a Pandas Series of 2D numpy arrays: 我有一个Pandas系列2D numpy数组:
import numpy as np
import pandas as pd
x1 = np.array([[0,1],[2,3],[3,4]],dtype=np.uint8)
x2 = np.array([[5,6],[7,8],[9,10]],dtype=np.uint8)
S = pd.Series(data=[x1,x2],index=['a','b'])
The output S should look like: 输出S应如下所示:
a [[0, 1], [2, 3], [3, 4]]
b [[5, 6], [7, 8], [9, 10]]
I wish to have it transformed into a Pandas DataFrame D where each column of the 2D numpy array in S becomes a 1D numpy array in a column of D: 我希望将它转换为Pandas DataFrame D,其中S中的2D numpy数组的每一列成为D列中的1D numpy数组:
D should look like: D应该看起来像:
0 1
a [0,2,3] [1,3,4]
b [5,7,9] [6,8,10]
Note, my actual data set is 1238500 arrays sized (32,8) so i was trying to avoid iterating over rows. 注意,我的实际数据集是1238500数组大小(32,8)所以我试图避免迭代行。
What is an efficient way to do this? 有效的方法是什么?
You can split and squeeze without ever converting the last dimension to a python list. 您可以拆分和挤压,而无需将最后一个维度转换为python列表。
df = S.apply(np.split, args=[2, 1]).apply(pd.Series).applymap(np.squeeze)
# 0 1
# a [0, 2, 3] [1, 3, 4]
# b [5, 7, 9] [6, 8, 10]
In args=[2, 1]
, 2
stands for the number of columns and 1
stands for the axis to slice across. 在
args=[2, 1]
, 2
代表列数, 1
代表轴切片。
Types: 类型:
In [280]: df.applymap(type)
Out[280]:
0 1
a <class 'numpy.ndarray'> <class 'numpy.ndarray'>
b <class 'numpy.ndarray'> <class 'numpy.ndarray'>
I would do like this: 我想这样做:
# flatten the list
S = S.apply(lambda x: [i for s in x for i in s])
# pick alternate values and create a data frame
S = S.apply(lambda x: [x[::2], x[1::2]]).reset_index()[0].apply(pd.Series)
# name index
S.index = ['a','b']
0 1
a [0, 2, 3] [1, 3, 4]
b [5, 7, 9] [6, 8, 10]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.