I have a CSV file that contains information about journeys taken by car.
I want to wrangle this data so that I have a single list for each journey (every row). That list should contain the journey_code as the first item in the list and then all the subsequent MGRS units as separate items. Lastly, I would want to have all those journey lists grouped into a parent list.
If I was doing this manually, it would look like this:
journeyCodeA = ['journeyCodeA', 'mgrs1', 'mgrs2', 'mgrs3']
journeyCodeB = ['journeyCodeB', 'mgrs2', 'mgrs4', 'mgrs7']
combinedList = [journeyCodeA, journeyCodeB]
Here is what I have so far to create a list per row and to combine the desired columns.
comparison_journey_mgrs = pd.read_csv(r"journey-mgrs.csv", delimiter = ',')
comparison_journey_mgrs['mgrs_grids'] = comparison_journey_mgrs['mgrs_grids'].str.replace(" ","")
comparison_journey_list = []
for index, rows in comparison_route_mgrs.iterrows():
holding_list = [rows.journey_code, rows.mgrs_grids]
comparison_journey_list.append(holding_list)
The problem with this is that it is treating the mgrs_grids column as a single string.
My list looks like this:
[['7211863-140','18TWL927129,18TWL888113,18TWL888113,...,18TWL903128']]
But I want it to looks like this:
[['7211863-140','18TWL927129', '18TWL888113', '18TWL888113',..., '18TWL903128']]
I am struggling to find a way to iterate over each row of the dataframe, reference the mgrs_grids column and then convert the comma-separated string to a list in-place.
Thanks for any help!
{'driver_code': {0: 7211863, 1: 7211863, 2: 7211863, 3: 7211863},
'journey_code': {0: '7211863-140',
1: '7211863-105',
2: '7211863-50',
3: '7211863-109'},
'mgrs_grids': {0: '18TWL927129,18TWL888113,18TWL888113,18TWL887113,18TWL888113,18TWL887113,18TWL887113,18TWL887113,18TWL903128',
1: '18TWL927129,18TWL939112,18TWL939112,18TWL939113,18TWL939113,18TWL939113,18TWL939113,18TWL939113,18TWL939113,18TWL960111,18TWL960112',
2: '18TWL927129,18TWL889085,18TWL889085,18TWL888085,18TWL888085,18TWL888085,18TWL888085,18TWL888085,18TWL890085',
3: '18TWL927129,18TWL952106,18TWL952106,18TWL952106,18TWL952106,18TWL952106,18TWL952106,18TWL952106,18TWL952105,18TWL951103'}}
