I can't access dataframe fields

MRAB python at mrabarnett.plus.com
Sat Feb 15 21:36:53 EST 2020


On 2020-02-16 00:50, Markos wrote:
> Hi all,
> 
> I created the following data frame (updated_distance_matrix)
> 
>        P1            P2            P4            P5           (P3, P6)
> P1 0,000000 0,244307 0,367696 0,341760 0
> P2 0.234307 0.000000 0.194165 0.1443178 0
> P4 0.366969 0.194165 0.000000 0.284253 0
> P5 0.341760 0.1443178 0.284253 0.000000 0
> (P3, P6) 0.000000 0.000000 0.000000 0.000000 0
> 
> I can change the fields of columns and rows P1-P5 without problems.
> 
> But when I try to access the fields of the row, or column, (P3, P6)
> 
> print (updated_distance_matrix_df.loc [clusters [i], last_cluster])
> 
> the message appears:
> 
> KeyError: 'the label [P3, P6] is not in the [index]'
> 
The rows and columns have "(P3, P6)", but you're looking for "[P3, P6]". 
They aren't the same.

> If I change find the "loc" by "at" method appears the error:
> 
> print (updated_distance_matrix_df.at [clusters [i], last_cluster])
> 
> TypeError: unhashable type: 'list'
> 
Is it expecting a tuple instead of a list? Tuples are hashable, lists 
are not.

> And if you simply leave:
> 
> print (updated_distance_matrix_df [clusters [i], last_cluster])
> 
> gives the error:
> 
> TypeError: unhashable type: 'list'
> 
> A last_cluster variable is of type list:
> 
> print (last_cluster, type (last_cluster))
> 
> ['P3', 'P6'] <class 'list'>
> 
> And a variable cluster [i] is a string:
> 
> print (clusters [i], type (clusters [i]))
> 
> P5 <class 'str'>
> 
> Any tip?
> 
> Thank you,
> 


More information about the Python-list mailing list