在 pandas
DataFrame
列中存储具有多个条目(固定长度)的项目的最佳方法是什么?我正在考虑类似 3D 位置矢量的东西。例如,如果我的 DataFrame
正在存储有关一堆物理对象的数据,它可能看起来像这样:
df = pandas.DataFrame({
'type': [1, 2, 1, 1, 3],
'mass': [1.1, 2.2, 3.3, 4.4, 5.5],
'pos': [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15]],
'vel': [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15]]
})
# mass pos type vel
# 0 1.1 [1, 2, 3] 1 [1, 2, 3]
# 1 2.2 [4, 5, 6] 2 [4, 5, 6]
# 2 3.3 [7, 8, 9] 1 [7, 8, 9]
# 3 4.4 [10, 11, 12] 1 [10, 11, 12]
# 4 5.5 [13, 14, 15] 3 [13, 14, 15]
这里,'pos'
和 'vel'
列是对象在 3D 空间中的位置和速度。
我想了几个选项,但似乎都不理想,甚至都不可行:
将 Python 列表存储为列中的值。这基本上就是我在上面的示例中展示的内容。不幸的是,这是非常低效的。
将列拆分为几个不同的列:
df = pandas.DataFrame({ 'type': [1, 2, 1, 1, 3], 'mass': [1.1, 2.2, 3.3, 4.4, 5.5], 'x': [1, 4, 7, 10, 13], 'y': [2, 5, 8, 11, 14], 'z': [3, 6, 8, 12, 15], 'vx': [1, 4, 7, 10, 13], 'vy': [2, 5, 8, 11, 14], 'vz': [3, 6, 8, 12, 15] }) # mass type vx vy vz x y z # 0 1.1 1 1 2 3 1 2 3 # 1 2.2 2 4 5 6 4 5 6 # 2 3.3 1 7 8 8 7 8 8 # 3 4.4 1 10 11 12 10 11 12 # 4 5.5 3 13 14 15 13 14 15
对于较大的属性,这似乎会变得很麻烦。但至少它是有效的?
我也试过将多维
numpy
数组分配给列,但不幸的是,pandas
拒绝了:pos = numpy.array([[11, 12, 13], [22, 23, 24], [33, 34, 35], [44, 45, 46], [55, 56, 57]]) df.loc[:, 'pos'] = pos # --------------------------------------------------------------------------- # ValueError Traceback (most recent call last) # <ipython-input-228-2ee95dd5aa19> in <module>() # ----> 1 df.loc[:, 'pos'] = pos # # /opt/anaconda-3/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py in __setitem__(self, key, value) # 177 key = com._apply_if_callable(key, self.obj) # 178 indexer = self._get_setitem_indexer(key) # --> 179 self._setitem_with_indexer(indexer, value) # 180 # 181 def _has_valid_type(self, k, axis): # # /opt/anaconda-3/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py in _setitem_with_indexer(self, indexer, value) # 561 value = np.array(value, dtype=object) # 562 if len(labels) != value.shape[1]: # --> 563 raise ValueError('Must have equal len keys and value ' # 564 'when setting with an ndarray') # 565 # # ValueError: Must have equal len keys and value when setting with an ndarray
最佳答案
我喜欢这个
d = pd.concat([
df[['mass', 'type']],
pd.DataFrame(df.pos.tolist(), df.index, ['x', 'y', 'z']),
pd.DataFrame(df.vel.tolist(), df.index, ['x', 'y', 'z'])
], axis=1, keys=['Scalar', 'Position', 'Velocity'])
d
Scalar Position Velocity
mass type x y z x y z
0 1.1 1 1 2 3 1 2 3
1 2.2 2 4 5 6 4 5 6
2 3.3 1 7 8 9 7 8 9
3 4.4 1 10 11 12 10 11 12
4 5.5 3 13 14 15 13 14 15
您可以从顶层轻松访问
d.Velocity
x y z
0 1 2 3
1 4 5 6
2 7 8 9
3 10 11 12
4 13 14 15
或者做数学题
(d.Velocity + d.Position).div(d.Scalar.mass, axis=0)
x y z
0 1.818182 3.636364 5.454545
1 3.636364 4.545455 5.454545
2 4.242424 4.848485 5.454545
3 4.545455 5.000000 5.454545
4 4.727273 5.090909 5.454545
您仍然可以轻松访问适当的 Numpy 数组
d.Position.values
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12],
[13, 14, 15]])
关于python - 在 pandas DataFrame 中存储多维属性(列),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52078235/