简体   繁体   中英

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

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)

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. I think what you want is the below, but it's hard to tell as I don't know what your new['Location'] column looks like:

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

 location

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM