i have a list like this:
first_list = [[ 1. , 45.4, 9.1],
[ 2. , 45.5, 9.1],
[ 2. , 45.4, 9.2],
[ 2. , 45.4, 9.2],
[ 3. , 45.4, 9.1],
[ 3. , 45.4, 9.1],
[ 3. , 45.4, 9.1] ]
I want to use the folio function HeatMapWithTime
, and to do that i need to group the data above according to the first item of each sublist (1., 2., 3. ecc):
new_list = [ [ [45.4, 9.1] ], # All coords for 1.
[ [45.5, 9.1], [45.4, 9.2], [45.4, 9.2] ], # All coords for 2.
[ [45.4, 9.1], [45.4, 9.1], [45.4, 9.2] ] ] # All coords for 3.
How can i do that?
One solution using pandas
, a wise choice when dealing with complex data formats:
import pandas as pd
pd.DataFrame(first_list).set_index(0).groupby(df.index).apply(lambda x: x.values.tolist()).tolist()
#->
[[[45.4, 9.1]],
[[45.5, 9.1], [45.4, 9.2], [45.4, 9.2]],
[[45.4, 9.1], [45.4, 9.1], [45.4, 9.1]]]