Home > OS >  Creating a List from a Data Frame in Python, is Adding the Index as Well
Creating a List from a Data Frame in Python, is Adding the Index as Well

Time:11-20

From a list of names I created (greater_three), I want to find all the names in that list in my DataFrame (new), and amend those values "location coordinates" in that DataFrame to a new list. But when I append I am also taking an index value.

    location = []
    for name in new['DBA Name']:
        if name in greater_three:
           location.append(new['Location'])
        else:
            pass
    Location

My output list (location) should like like this:

[[41.7770923949, -87.6060037796],
[41.7770923949, -87.6060037796],
[41.7770923949, -87.6060037796],

But I am getting it with an Index like this:

[0     (41.777092394888655, -87.60600377956905)
 1       (41.78457591499572, -87.6547753761994)
 2       (41.74427989606148, -87.5716351762223)
 3       (41.69164609748754, -87.6422140544927)

Also, smaller issue but I'm curious, it is iterating many times through (after I removed all the duplicate names from the data frame) like below, it should only have length of 26 coordinates (25 including 0):

 22    (41.901086765978654, -87.74854019856667)
 23     (41.70774046981763, -87.64300283870763)
 24     (41.75937734623751, -87.66111539963164)
 25     (41.75655095611123, -87.61068980246957)
 Name: Location, dtype: object,
 0     (41.777092394888655, -87.60600377956905)
 1       (41.78457591499572, -87.6547753761994)
 2       (41.74427989606148, -87.5716351762223)
...
23     (41.70774046981763, -87.64300283870763)
24     (41.75937734623751, -87.66111539963164)
25     (41.75655095611123, -87.61068980246957)
 Name: Location, dtype: object,
 0     (41.777092394888655, -87.60600377956905)
 1       (41.78457591499572, -87.6547753761994)
 2       (41.74427989606148, -87.5716351762223)
 3       (41.69164609748754, -87.6422140544927)

My columns look like this, I just need the coordinates in a list, I can take from either: 'Longitutude'and 'Latitude' or 'Location'.

enter image description here

CodePudding user response:

For every name, you're just re-appending the whole column to your list, rather than just the entries correspond to each name in your loop. You can fix this using .loc and filtering on where the names match. You should also .drop_duplicates in your new['DBA Name'] to go through to avoid appending the same thing several times. Also your else: pass is not required. Your location column is tuples, but you want the output to be a list, so you'll need to covert that. See below:

 location = []
 for name in new['DBA Name'].drop_duplicates():
        if name in greater_three:
           location.append(list(new.loc[new['DBA Name'] == name, 'Location'].iloc[0]))

 location
  • Related