Type splitting error in pandas dataframe

I have foll. pandas dataframe:

df.shape (86, 245) 

However, when I do this:

 df[0, :] 

I get an error message:

 *** TypeError: unhashable type 

How to fix it? I just want to get the first row

+5
source share
1 answer

If you need the first line as Series , just use DataFrame.iloc :

 df.iloc[0, :] 

But if necessary DataFrame use a iloc , use iloc , but add [] or use head :

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

Example:

 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) ABCDEF 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)) ABCDEF 0 1 4 7 1 5 7 print (df.iloc[[0], :]) ABCDEF 0 1 4 7 1 5 7 
+6
source

Source: https://habr.com/ru/post/1263176/


All Articles