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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With