0

I used this code to get data from pytrends:

import pandas as pd
from pytrends.request import TrendReq
keywrdList = ["Apple", "Google"]

pytrend = TrendReq(hl='en-US', tz=360)
pytrend.build_payload(kw_list= keywrdList, timeframe = 'now 7-d')
df = pytrend.interest_over_time()
df2 = df.drop(['isPartial'], axis=1)
  
newList = []
oldList = []
for item in df2['date']:
    oldList.append(item)
    newList.append(item.split()[0])
df2['date'] = df2['date'].replace(oldList, newList)
print(df2)

I get the following dataframe on printing:

           date  Apple  Google
0    2023-02-13      8      89
1    2023-02-13      9      91
2    2023-02-13      9      88
3    2023-02-13      9      81
4    2023-02-13      9      85

I want data in the format:

 Company Date        Value
 Apple   2023-02-13  30
 Apple   2023-02-13  30
 Apple   2023-02-13  45
 Google  2023-02-13  29

How can I manipulate the dataframe the get result in following format? pd.pivot()is not giving correct format.

Sam
  • 141
  • 1
  • 2
  • 9

0 Answers0