Pandas loc error: 'Series' objects are mutable, thus they cannot be hashed

Pandas loc error: 'Series' objects are mutable, thus they cannot be hashed

我需要一些帮助来解决处理 pandas DataFrame 的问题。这是代码:

df.drop(df.index[0], inplace=True)
df.columns = ['Mic. No.', 'X', 'Y', 'Z', 'Re. Pre.', 'Im. Pre.']
df['Pre'] = df['Re. Pre.'] + df['Im. Pre.'] * 1j
df.drop(['Mic. No.', 'Re. Pre.', 'Im. Pre.'], axis=1, inplace=True)

if z != 0:
   df = df.loc(df['Z'] == z)

我在熊猫数据框中加载了一个 excel sheet。现在经过一些预处理后,DataFrame 的形式如下:

          X         Y     Z                       Pre
  1      0.16      0.16  0.05   (1.0048704-0.51310315j)
  2      0.16     -0.16  0.05   (0.24814222-1.6094971j)
  3     -0.16      0.16  0.05   (0.24815122-1.6094059j)
  4     -0.16     -0.16  0.05   (1.0048704-0.51310315j)
  5 -0.154993  0.154993  0.05  (-0.13939651-1.7231593j)

现在我想删除 DataFrame 中所有列“Z”中没有值 z 的列。我收到错误:“TypeError:'Series' 对象是可变的,因此它们不能被散列”。我不知道该怎么做,因为我对它的看法与 pandas 文档中的完全一样。 https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.loc.html

df.loc[df['shield'] > 6]
               max_speed  shield
 sidewinder          7       8

你也可以用 lambda 表达式来做:

df = df[lambda x: x['Z'] == z]

您可以在 df.loc:

中使用方括号
df = df.loc[df['Z'] == z]