Commit dce9d2d9 authored by CARLOS JESUS ALCOBA CABEZAS's avatar CARLOS JESUS ALCOBA CABEZAS
Browse files

Upload New File

parent 5b2d7673
%% Cell type:markdown id:compliant-relations tags:
# PYTRENDS
%% Cell type:code id:earned-characteristic tags:
``` python
#import the libraries
import pandas as pd
from pytrends.request import TrendReq
import pytrends
import matplotlib.pyplot as plt
import time
from os import remove
from os import path
```
%% Cell type:code id:neutral-medicine tags:
``` python
kw_list=['Djokovic','Rafa Nadal','Open Australia']
start=True
```
%% Cell type:code id:quarterly-north tags:
``` python
pytrend = TrendReq()
df = pytrend.top_charts(2021, hl='en-US', tz=300, geo='ES')#ejemplo global
df.head(20)
```
%% Output
title exploreQuery
0 tiempo mañana
1 Eurocopa
2 La Liga
3 Real Madrid
4 Roland Garros
5 Volcán La Palma
6 Bonoloto
7 Mbappé
8 Atlético de Madrid
9 NBA
%% Cell type:code id:martial-rough tags:
``` python
#search interest per region
#creamos un modelo con la lista de palabras
pytrend.build_payload(kw_list, timeframe='today 1-m',geo='ES')
# Interest by Region
regiondf = pytrend.interest_by_region()
# miramos las filas donde el valor no sea cero
regiondf = regiondf[(regiondf != 0).all(1)]
#quitamos los valores nulos
regiondf.dropna(how='all',axis=0, inplace=True)
#visualizamos
regiondf.plot(figsize=(20, 12), y=kw_list, kind ='bar')
```
%% Output
<AxesSubplot:xlabel='geoName'>
%% Cell type:code id:assigned-kansas tags:
``` python
pytrend = TrendReq()
startTime = time.time()
dataset = []
pytrend.build_payload(
kw_list=kw_list,geo='ES',
cat=0,
timeframe='now 7-d')
data = pytrend.interest_over_time()
if not data.empty:
data = data.drop(labels=['isPartial'],axis='columns')
dataset.append(data)
result = pd.concat(dataset, axis=1)
result.to_csv('search_trends.csv')
```
%% Cell type:code id:serious-configuration tags:
``` python
df=pd.read_csv('search_trends.csv')
plt.figure(figsize=(30,10))
for i in range (0,len(kw_list)):
plt.plot(df["date"],df[kw_list[i]])
plt.legend(kw_list)
```
%% Output
<matplotlib.legend.Legend at 0x7f97ac8b7d90>