Python: Applying function to list of tems
<p>I have following code snippet that helps me to get Google Trends data (see <a href="https://github.com/GeneralMills/pytrends" rel="nofollow">https://github.com/GeneralMills/pytrends</a>):</p>

<pre><code>trend_payload = {'q': 'Dogs, Cats, Catfood, Dogfood','date': '01/2015 12m'}
trend = pytrend.trend(trend_payload)
df = pytrend.trend(trend_payload, return_type='dataframe')
df
</code></pre>

<p>As this query has the disadvantage that Google Trends normalizes all data based on the queried data, I prefer to make each a single call and chain the df next to each other. I thought about a function like this:</p>

<pre><code>queries = ['Cats', 'Dogs', 'Catfood','Dogfood']

function(queries)
    trend_payload = {'q': queries, 'date': '01/2015 12m'}
    trend = pytrend.trend(trend_payload)
    df = pytrend.trend(trend_payload, return_type='dataframe')

# then put every df of each query next to each other
</code></pre>

<p>How can I do this?</p>

<p>You can work on this: </p>

<pre><code>queries = ['Cats', 'Dogs', 'Catfood','Dogfood']

def function(queries):
    trend_payload = {'q': queries, 'date': '01/2015 12m'}
    trend = pytrend.trend(trend_payload)
    df = pytrend.trend(trend_payload, return_type='dataframe')
    return df 

list_of_df = [function([query,]) for query in queries] 
</code></pre>

<p>then you have to <code>concat</code> the data frames in the list. </p>

<p>More elegantly you can call: </p>

<pre><code>list_of_df = map(function, queries)
</code></pre>

<p>in this case you should rewrite <code>function</code> so that it accepts a single item.
If you don't want to modify <code>function</code> you can write this: </p>

<pre><code>list_of_df = map(lambda x: function([x,]), queries) 
</code></pre>

python function pandas dataframe