Pandas dataframe values return Nonetype


Keywords:python 


Question: 

I'm trying out this Python API for google trends. it created a pandas dataframe where the first column is the date and the others the keywords in kw_list, the values representing how much people search for the keywords.

This is my code

from pytrends.request import TrendReq


# Login to Google. Only need to run this once, the rest of requests will use the same session.
pytrend = TrendReq()

# Create payload and capture API tokens.

pytrend.build_payload(kw_list=['adele', 'wat'])

interest_over_time_df = pytrend.interest_over_time()

c = print(interest_over_time_df.iloc[-1]['adele'])
print(c)

This outputs '5'. However, the type is Nonetype, so I can't compare this value to other values. How can I get the output as an integer?


1 Answer: 

Your c as you have it now is a print statement, which is indeed a NoneType.

Try replacing

c = print(interest_over_time_df.iloc[-1]['adele'])
print(c)

with

c = int(interest_over_time_df.iloc[-1]['adele'])
print(c)

This prints c, which is explicitly cast as an int. Depending on how interest over time is calculated, you might actually want a float (not an int), so that you can compare for instance 1.0 to 1.1 if such values are possible. if that is the case, you can just replace int in my suggestion with float