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>I would simply concatenate DFs as <a href="http://stackoverflow.com/questions/40102772/python-applying-function-to-list-of-tems#comment67478072_40102772">jimifiki has already proposed</a>:</p>

<pre><code>df = pd.concat([pytrend.trend({'q': x, 'date': '01/2015 12m'},
                              return_type='dataframe')
                for x in queries], axis=1)
</code></pre>

<p>or in function:</p>

<pre><code>def get_trends(queries, dt):
    return pd.concat([pytrend.trend({'q': x, 'date': dt},
                                    return_type='dataframe')
                      for x in queries], axis=1)

df = get_trends(queries, '01/2015 12m')
</code></pre>

<p>Demo:</p>

<pre><code>In [24]: df = get_trends(queries, '01/2015 12m')

In [25]: df
Out[25]:
            cats   dogs  catfood  dogfood
Date
2015-01-04  74.0   85.0     65.0     47.0
2015-01-11  74.0   84.0     60.0     52.0
2015-01-18  72.0   82.0     49.0     57.0
2015-01-25  69.0   78.0     45.0     37.0
2015-02-01  73.0   77.0     51.0     52.0
...          ...    ...      ...      ...
2015-11-29  83.0   80.0     47.0     49.0
2015-12-06  80.0   79.0     70.0     50.0
2015-12-13  83.0   84.0     67.0     49.0
2015-12-20  89.0   91.0     61.0     58.0
2015-12-27  90.0  100.0     58.0     45.0

[52 rows x 4 columns]
</code></pre>

python function pandas dataframe