转换 pandas “成对阵列系列"到“两栏式DataFrame"? [英] Convert a pandas "Series of pair arrays" to a "two-column DataFrame"?
问题描述
我有一个Pandas系列,它由成对的阵列组成:
I have a Pandas Series that consists of arrays of pairs:
In [177]: pair_arrays
Out[177]:
15192 [[1, 9], [2, 14], [4, 1], [5, 36], [6, 8], [7,...
16012 [[0, 107], [1, 42], [2, 22], [3, 59], [4, 117]...
17523 [[0, 44], [1, 36], [2, 43], [3, 28], [4, 52], ...
...
我想将其重塑为具有两列"x"和"y"的数据框,其形状类似于:
I would like to reshape that into a dataframe with two columns, 'x' and 'y', which has a shape similar to:
In [179]: pd.DataFrame([{'x':1, 'y':42}, {'x':4, 'y':12}], columns=['x', 'y'])
Out[179]:
x y
0 1 42
1 4 12
...
我该怎么做?
推荐答案
假定系列中的每个元素都是对的数组,并且每个对都是一个序列,这应该可以工作:
Assuming that each element in the series is an array of pairs, and each pair is a sequence, this should work:
pair_df = pd.DataFrame(np.vstack(pair_arrays.values), columns=['x','y'])
关键是熊猫不知道如何使用对象数组.因此,我在这里所做的就是将其转换为对象数组的numpy数组.然后,我将堆叠对象数组,这将为您提供2D整数数组,然后将其转换回DataFrame.
The key point is that pandas doesn't know how to work with object arrays. So what I am doing here is converting it to a numpy array of object arrays. Then I am stacking the object arrays, which gets you a 2D integer array, and then converting it back to a DataFrame.
从技术上讲,您当前不需要使用values
方法来显式转换为numpy数组,但是我认为这更清晰,并且从长期来看可能更安全.
Technically you don't currently need to use the values
method to explicitly convert to a numpy array, but I think that is clearer and potentially safer long-term.
这篇关于转换 pandas “成对阵列系列"到“两栏式DataFrame"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!