Home > front end >  How can I create a pandas column based on another pandas column that has for values a list?
How can I create a pandas column based on another pandas column that has for values a list?

Time:07-25

I am working with a dataframe and one of the columns has for values a list of strings in each row. The list contains a number of links (each list can have a different number of links). I want to create a new column that will be based on this column of lists but keep only the links that have the keyword "uploads".

To my example, the first entry of the column is like that:

['https://seekingalpha.com/instablog/5006891-hfir/4960045-natural-gas-daily',
 'https://seekingalpha.com/article/4116929-weekly-natural-gas-storage-report',
 'https://static.seekingalpha.com/uploads/2017/10/26/5006891-15090647719993095_origin.png',
 'https://static.seekingalpha.com/uploads/2017/10/26/5006891-15090647854075453_origin.png',
 'https://static.seekingalpha.com/uploads/2017/10/26/5006891-1509065004154725_origin.png',
 'https://seekingalpha.com/account/research/subscribe?slug=hfir-energy&sasource=upsell']

And I want to keep only

['https://static.seekingalpha.com/uploads/2017/10/26/5006891-15090647719993095_origin.png',
 'https://static.seekingalpha.com/uploads/2017/10/26/5006891-15090647854075453_origin.png',
 'https://static.seekingalpha.com/uploads/2017/10/26/5006891-1509065004154725_origin.png']

And put the clean version in a new column of the same dataframe.

Can you please suggest a way to do it?

CodePudding user response:

I just found a way where I create a function that looks within a list for a specific pattern (in my case the keyword "uploads")

def clean_alt_list(list_):
    list_ = [s for s in list_ if "uploads" in s]
    return list_

And then I apply this function into the column I am interested in

df['clean_links'] = df['links'].apply(clean_alt_list)

CodePudding user response:

IIUC, this should work for you:

df = pd.DataFrame({'url': [['https://seekingalpha.com/instablog/5006891-hfir/4960045-natural-gas-daily', 'https://seekingalpha.com/article/4116929-weekly-natural-gas-storage-report', 'https://static.seekingalpha.com/uploads/2017/10/26/5006891-15090647719993095_origin.png', 'https://static.seekingalpha.com/uploads/2017/10/26/5006891-15090647854075453_origin.png', 'https://static.seekingalpha.com/uploads/2017/10/26/5006891-1509065004154725_origin.png', 'https://seekingalpha.com/account/research/subscribe?slug=hfir-energy&sasource=upsell']]})
df = df.explode('url').reset_index(drop=True)
df[df['url'].str.contains('uploads')]

Result:

                                                 url
2  https://static.seekingalpha.com/uploads/2017/1...
3  https://static.seekingalpha.com/uploads/2017/1...
4  https://static.seekingalpha.com/uploads/2017/1...
  • Related