I have a dataframe as follows:
df
KEY NAME ID_LOCATION _GEOM
0 61196 name1 [(u'-88.121429', u'41.887726')] [[[lon00,lat00],[lon01, lat01]]]
1 61197 name2 [(u'-75.161934', u'38.725163')] [[[lon10,lat10], [lon11,lat11],...]]
2 61199 name3 [(u'-88.121429', u'41.887726'), (-77.681931, 37.548851)] [[[lon20, lat20],[lon21, lat21]]]
where id_loc is a list of tuples. How can I groupby id_loc
in a way that if there is a matching (lon, lat) pair
, merge those 2 rows and other columns by separated by comma.
expected_output_df
KEY NAME ID_LOCATION _GEOM
0 61196,61199 name1,name3 [(u'-85.121429', u'40.887726'), (-77.681931, 37.548851)] [[[lon00, lat00],[lon01, lat01],[lon20, lat20],[lon21, lat21]]]
1 61197 name2 [(u'-72.161934', u'35.725163')] [[[lon10,lat10], [lon11,lat11],...]]
I tried the following but no success and gives me error as unhashable type list
:
def f(x):
return pd.Series(dict(KEY='{%s}' % ', '.join(x['KEY']),
NAME='{%s}' % ', '.join(x['NAME']),
ID_LOCATION='{%s}' % ', '.join(x['ID_LOCATION']),
_GEOM='{%s}' % ', '.join(x['_GEOM']))
)
df = df.groupby('ID_LOCATION').apply(f)