如何从单个数据框列中减去数组?

时间:2020-05-07 13:36:38

标签: python arrays pandas

我有两个数字的“向量”,我想从另一个中减去一个。我的问题是其中之一是列表的数组:

array([[ 796.24475 ],
      [ -17.138123],
      [ 164.9989  ],
      ...,
      [-469.85388 ],
      [-762.1892  ],
      [-451.34702 ]], dtype=float32)

而另一个是熊猫数据框的一列:

0       831.871558
21       26.070256
25      199.351116
28      861.052529
35      113.232070
           ...    
9440   -163.200046
9448   -893.619023
9449   -439.174531
9451   -795.033901
9461   -413.469417
Name: electricity, Length: 1895, dtype: float64 

它们都具有相同数量的数据点,当我尝试从另一个中减去一个时,我得到的错误如下:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Projects\test\testvenv\lib\site-packages\pandas\core\series.py", line 636, in __array_ufunc__
    self, ufunc, method, *inputs, **kwargs
  File "pandas\_libs\ops_dispatch.pyx", line 91, in pandas._libs.ops_dispatch.maybe_dispatch_ufunc_to_dunder_op
  File "C:\Projects\test\testvenv\lib\site-packages\pandas\core\ops\common.py", line 64, in new_method
    return method(self, other)
  File "C:\Projects\test\testvenv\lib\site-packages\pandas\core\ops\__init__.py", line 502, in wrapper
    return _construct_result(left, result, index=left.index, name=res_name)
  File "C:\Projects\test\testvenv\lib\site-packages\pandas\core\ops\__init__.py", line 475, in _construct_result
    out = left._constructor(result, index=index)
  File "C:\Projects\test\testvenv\lib\site-packages\pandas\core\series.py", line 305, in __init__
    data = sanitize_array(data, index, dtype, copy, raise_cast_failure=True)
  File "C:\Projects\test\testvenv\lib\site-packages\pandas\core\construction.py", line 482, in sanitize_array
    raise Exception("Data must be 1-dimensional")
Exception: Data must be 1-dimensional

感谢所有帮助,在此先感谢!

2 个答案:

答案 0 :(得分:2)

我认为最简单的选择是选择一维数组(DataFrame和数组的长度必须相同):

df['electricity'] - arr[:, 0]

另一个想法,谢谢@timgeb是使用numpy.squeeze

df['electricity'] - arr.squeeze()

答案 1 :(得分:1)

我建议您将数组转换为熊猫系列。 (如果它是一个numpy数组,则可以如下所示进行操作)

series = pd.Series(np_array)

(必要时请确保首先重塑np.array)

然后您可以从pandas数据框中的列中减去此系列,如下所示:

df['col_name'] - series