Unhashable type error in pandas dataframe

Question:

I have the following pandas dataframe:

df.shape

(86, 245)

However, when I do this:

df[0, :]

I get the error:

*** TypeError: unhashable type

How do I fix this? I just want to get the first row

Asked By: user308827

||

Answers:

If need first row as Series just use DataFrame.iloc:

df.iloc[0, :]

But if need DataFrame use iloc but add [] or use head:

df.iloc[[0], :]
df.head(1)

Sample:

df = pd.DataFrame({'A':[1,2,3],
                   'B':[4,5,6],
                   'C':[7,8,9],
                   'D':[1,3,5],
                   'E':[5,3,6],
                   'F':[7,4,3]})

print (df)
   A  B  C  D  E  F
0  1  4  7  1  5  7
1  2  5  8  3  3  4
2  3  6  9  5  6  3

print (df.iloc[0, :])
A    1
B    4
C    7
D    1
E    5
F    7
Name: 0, dtype: int64

print (df.head(1))
   A  B  C  D  E  F
0  1  4  7  1  5  7

print (df.iloc[[0], :])
   A  B  C  D  E  F
0  1  4  7  1  5  7
Answered By: jezrael

I had TypeError: unhashable type: 'Series while trying to use .loc function over a ps.Dataframe.

My mistake was to use .loc as .loc(...) when it is supposed to be .loc[...]

Example of the mistake:

import pandas as pd

df = pd.DataFrame({'A':[1,2,3],
                   'B':[4,5,6],
                   'C':[7,8,9],
                   'D':[1,3,5],
                   'E':[5,3,6],
                   'F':[7,4,3]})

print (df)

# typical mistake
print(df.loc(
    df["A"] == 1
))

result:

PS C:Usersrascoussierpythonstackoverflowtype_error_with_df_loc> C:UsersrascoussierAnaconda3envselastic-1python.exe .main.py                                 
   A  B  C  D  E  F
0  1  4  7  1  5  7
1  2  5  8  3  3  4
2  3  6  9  5  6  3
Traceback (most recent call last):
  File "C:Usersrascoussierpythonstackoverflowtype_error_with_df_locmain.py", line 12, in <module>
    print(df.loc(
  File "C:UsersrascoussierAnaconda3envselastic-1libsite-packagespandascoreindexing.py", line 634, in __call__
    axis = self.obj._get_axis_number(axis)
  File "C:UsersrascoussierAnaconda3envselastic-1libsite-packagespandascoregeneric.py", line 550, in _get_axis_number
    return cls._AXIS_TO_AXIS_NUMBER[axis]
TypeError: unhashable type: 'Series'

This mistake is not detected by Pylance on VSCode. I don’t know why. But below is the correction.

import pandas as pd

df = pd.DataFrame({'A':[1,2,3],
                   'B':[4,5,6],
                   'C':[7,8,9],
                   'D':[1,3,5],
                   'E':[5,3,6],
                   'F':[7,4,3]})

print (df)

# correction
print("Correction works, see below: ")
print(df.loc[
    df["A"] == 1
])

result:

PS C:Usersrascoussierpythonstackoverflowtype_error_with_df_loc> C:UsersrascoussierAnaconda3envselastic-1python.exe .main.py
   A  B  C  D  E  F
0  1  4  7  1  5  7
1  2  5  8  3  3  4
2  3  6  9  5  6  3
Correction works, see below:
   A  B  C  D  E  F
0  1  4  7  1  5  7

Remark: My example is slightly different from the one asked by the question, but since I found this solution while searching, it might help others with the same issue.

Answered By: Onyr
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.