Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unhashable type error in pandas dataframe

Tags:

python

pandas

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

like image 648
user308827 Avatar asked Jan 22 '17 21:01

user308827


1 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
like image 56
jezrael Avatar answered Nov 08 '22 22:11

jezrael