4

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

2 Answers 2

3

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))
Sign up to request clarification or add additional context in comments.

Comments

3

You can use apply:

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

Or assign:

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

Or list comprehension:

df['url'] = [get_url(x) for x in df['name']]

3 Comments

Glad can help. Do you use from another answer second solution?
The solution I chose added the return() function. It's just a piece I missed...
Ok, I missed it also ;) Nice day!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.