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 url
getting printed one by one. But I trying to expand the dataframe. I tried my luck with itterows
and df.loc
but so far it didn't work out. Any ideas? Thanks
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With