我有以下代码段,可帮助我获取Google趋势数据(请参阅https://github.com/GeneralMills/pytrends):
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
由于此查询的缺点是Google趋势会根据查询的数据对所有数据进行规范化,因此我更倾向于将每个数据调整为单个调用并将df链接到彼此旁边。我想到了这样一个函数:
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
我该怎么做?
答案 0 :(得分:1)
你可以解决这个问题:
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]
然后你必须concat
列表中的数据框。
更优雅的是,您可以致电:
list_of_df = map(function, queries)
在这种情况下,您应该重写function
,以便它接受单个项目。
如果您不想修改function
,可以写下:
list_of_df = map(lambda x: function([x,]), queries)
答案 1 :(得分:1)
我只是将DF连接为jimifiki has already proposed:
df = pd.concat([pytrend.trend({'q': x, 'date': '01/2015 12m'},
return_type='dataframe')
for x in queries], axis=1)
或在功能中:
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')
演示:
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]