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

13k Views Asked by At

I need some help with a problem in handling pandas DataFrames. Here is the Code:

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)

I loaded an excel sheet in a panda dataframe. Now after some preprocessing the DataFrame is in the following form:

          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)

now I want to delete all columns in the DataFrame which don't have the Value z in the column "Z". I get the error: "TypeError:'Series' objects are mutable, thus they cannot be hashed". I don't know what to do because how I see it it's exactly the same like in the pandas documentation. https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.loc.html

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

There are 2 best solutions below

0
On BEST ANSWER

you can also do it with lambda expression:

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

You can use square brackets with df.loc:

df = df.loc[df['Z'] == z]