DataFrame.apply(func,raw = True)似乎没有生效?

时间:2018-05-18 09:33:37

标签: pandas

我想把我的数据帧df的几列只散列在一起,所以我做

temp = df['field1', 'field2]
df["hash"] = temp.apply(lambda x: hash(x), raw=True, axis = 1)

我将raw设置为true,因为doc(我使用的是0.22)说它会传递numy数组而不是可变的serie https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.apply.html#pandas.DataFrame.apply但是即使使用raw = True我也会得到一个Serie?

  File "/nix/store/9ampki9dbq0imhhm7i27qkh56788cjpz-python3.6-pandas-0.22.0/lib/python3.6/site-packages/pandas/core/frame.py", line 4877, in apply
    ignore_failures=ignore_failures)
  File "/nix/store/9ampki9dbq0imhhm7i27qkh56788cjpz-python3.6-pandas-0.22.0/lib/python3.6/site-packages/pandas/core/frame.py", line 4973, in _apply_standard
    results[i] = func(v)
  File "/home/teto/mptcpanalyzer/mptcpanalyzer/data.py", line 190, in _hash_row
    return hash(x)
  File "/nix/store/9ampki9dbq0imhhm7i27qkh56788cjpz-python3.6-pandas-0.22.0/lib/python3.6/site-packages/pandas/core/generic.py", line 1045, in __hash__
    ' hashed'.format(self.__class__.__name__))
TypeError: ("'Series' objects are mutable, thus they cannot be hashed", 'occurred at index 1')

1 个答案:

答案 0 :(得分:1)

这很奇怪,因为我无法重现您的确切错误(也就是说,raw=True确实会导致np.ndarray被传递。无论如何,Seriesnp.ndarray都不是可以播放的。但是,以下工作:

temp.apply(lambda x: hash(tuple(x)), axis=1)

tuple 可以播放。