5

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
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Samuel K.
  • 180
  • 1
  • 1
  • 11
  • i got a solution: df = df[df['Z'] == z] But can someone explain why the other way wont work? – Samuel K. Oct 11 '20 at 15:12
  • Sorry, my answer was not what you need? – Mehdi Golzadeh Oct 11 '20 at 20:31
  • 1
    it was! but as i said i got a solution and from this point i was more interested in the reson my first attempt didnt work. – Samuel K. Oct 13 '20 at 05:47
  • 1
    Samuel, I also was stuck with same eror, it seems not intuitive. The issue is that your initial code created circular reference, changing values that affected the selection. So proper solution is to create mask first and then apply change. See more details in this answer: https://stackoverflow.com/a/49356823/10396469. `mask = df[df['feature']==0].index; df.loc[mask, 'feature'] = z` – Poe Dator Jul 03 '21 at 10:22
  • @SamuelK. At the [documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.loc.html) .loc is used with brackets. I could say that it is because in Python parenthesis means tuple that is immutable while brackets means list that are mutable. Take a look [here](https://stackoverflow.com/questions/4407873/whats-the-difference-between-vs-vs) – KPC Jan 25 '23 at 10:52

2 Answers2

6

You can use square brackets with df.loc:

df = df.loc[df['Z'] == z]
He3lixxx
  • 3,263
  • 1
  • 12
  • 31
RenzeH
  • 61
  • 1
  • 2
3

you can also do it with lambda expression:

df = df[lambda x: x['Z'] == z]
Mehdi Golzadeh
  • 2,594
  • 1
  • 16
  • 28