Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying a function to pandas dataframe and add a new column?

I got a simple dataframe. It basically looks like this - only much larger.

   import pandas as pd
    csv = [{"name" : "Peters Company", "Apples" : 1}, {"name" : "Quagmires Company", "Apples" : 0}]
    df = pd.DataFrame(csv)

I trying to apply a little function I wrote to the name column. Here is what I do:

from google import search
def get_url(query):
    url = search(query, tld='com', num=1, stop=0, pause=10)
    print(next(url))

I am using google to search for a certain query and print it afterwords. I am trying to create a new column url which contains the result of get_url row by row.

Here is what I did:

for i in df.name:
    get_url(i) 

Obviously, this only results in the urlgetting printed one by one. But I trying to expand the dataframe. I tried my luck with itterows and df.locbut so far it didn't work out. Any ideas? Thanks

like image 410
Rachel Avatar asked Sep 05 '25 05:09

Rachel


1 Answers

The apply method is exactly what you want. All you need to do is to add a return value to your function:

def get_url(query):
    url = search(query, tld='com', num=1, stop=0, pause=10)
    return next(url) 

df['url'] = df['name'].apply(get_url)

If you want to pass other parameters in addition to the name cell, you can use lambda:

def get_url(query, another_param):
        url = search(query, tld='com', num=1, stop=0, pause=10)
        return next(url) 

df['url'] = df['name'].apply(lambda column_name: get_url(column_name, another_value))
like image 73
AndreyF Avatar answered Sep 07 '25 19:09

AndreyF