My goal is to create a dataframe based on the 'viewCount', 'likeCount', 'favoriteCount' and 'commentCount'.
The code below works gives 25 values for 'viewCount', 'favoriteCount', and 'commentCount', but I get an error for 'likeCount' because 'likeCount' has 1 missing value in its list.
How can I create an "if, then statement" that will place a 0 count into the 'likeCount' list when the programs see's that value doesn't exist.
The if then statement below is what I've tried, but I'm getting a KeyError: 'likeCount' error
x=0
y=0
viewCount = []
likeCount = []
favoriteCount = []
commentCount = []
while (x < len(response2)):
viewCount.append(response2[x]['items'][0]['statistics']['viewCount'])
favoriteCount.append(response2[x]['items'][0]['statistics']['favoriteCount'])
commentCount.append(response2[x]['items'][0]['statistics']['commentCount'])
if not (response2[x]['items'][0]['statistics']['likeCount']):
likeCount.append(0)
else:
likeCount.append(response2[x]['items'][0]['statistics']['likeCount'])
x=x+1
#print(video_title, video_id)
df2 = pd.DataFrame({'viewCount': viewCount,'favoriteCount': favoriteCount,
'commentCount': commentCount, 'likeCount' : likeCount} )
df2
ERROR MESSAGE:
KeyError Traceback (most recent call last)
10 favoriteCount.append(response2[x]['items'][0]['statistics']['favoriteCount'])
11 commentCount.append(response2[x]['items'][0]['statistics']['commentCount'])
---> 12 if not (response2[x]['items'][0]['statistics']['likeCount']):
13 likeCount.append(0)
14 else:
KeyError: 'likeCount'