Search code examples
pandasdataframepandas-loc

replacing values with .loc


I am trying to loop through a DF column and get just the URL key from a column of strings. I have printed every stage of this loop and it works correctly. However when printing the DF after the loop I only see the column I create with the correct values and the loop has now replaced other values in those rows a NaN. what am i doing wrong here? #clean playlists link for just key and create new column 'key'

for i, value in enumerate(tats['Playlist Link'].values):
    splitstring = str(value).split('/')
    value = splitstring[len(splitstring)-1]
    if "?" in value:
        value = value[0:value.index('?')]
    else:
        value = value
    tats.loc[i, 'key'] = value   

Solution

  • Try:

    for i, value in zip(tats.index, tats['Playlist Link'].values):
        ...