Re: What I learned today
Stefan Ram wrote:
> The other thing I read in a book. I already knew that one
> can zip using ... »zip«. E.g.,
>
> x =( 'y', 'n', 'a', 'n', 't' )
> y =( 4, 2, 7, 3,1 )
> z = zip( x, y )
> print( list( z ))
> [('y', 4), ('n', 2), ('a', 7), ('n', 3), ('t', 1)]
>
> But the book told me that you can unzip using ... »zip« again!
>
> z = zip( x, y )
> a, b = zip( *z )
> print( a )
> ('y', 'n', 'a', 'n', 't')
> print( b )
> (4, 2, 7, 3, 1)
>
> Wow!
>
Another way to look at that is that if you write a matrix as a tuple of
tuples
>>> a = (1,2), (3,4), (5,6)
you can transpose it with
>>> def transposed(a):
... return tuple(zip(*a))
...
>>> transposed(a)
((1, 3, 5), (2, 4, 6))
and transposing twice gives the original matrix:
>>> transposed(transposed(a)) == a
True
--
https://mail.python.org/mailman/listinfo/python-list
I can't access dataframe fields
Hi all, I created the following data frame (updated_distance_matrix) P1 P2 P4 P5 (P3, P6) P1 0,00 0,244307 0,367696 0,341760 0 P2 0.234307 0.00 0.194165 0.1443178 0 P4 0.366969 0.194165 0.00 0.284253 0 P5 0.341760 0.1443178 0.284253 0.00 0 (P3, P6) 0.00 0.00 0.00 0.00 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]' 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' 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'] And a variable cluster [i] is a string: print (clusters [i], type (clusters [i])) P5 Any tip? Thank you, Markos -- https://mail.python.org/mailman/listinfo/python-list
Re: I can't access dataframe fields
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,00 0,244307 0,367696 0,341760 0 P2 0.234307 0.00 0.194165 0.1443178 0 P4 0.366969 0.194165 0.00 0.284253 0 P5 0.341760 0.1443178 0.284253 0.00 0 (P3, P6) 0.00 0.00 0.00 0.00 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'] And a variable cluster [i] is a string: print (clusters [i], type (clusters [i])) P5 Any tip? Thank you, -- https://mail.python.org/mailman/listinfo/python-list
