问题描述
所以我有一个包含用户,日期和帖子列的数据集。我正在尝试为每个用户在post列中生成食物包含的卡路里的列。该数据集的长度为21,下面的代码查找食物单词,获取其卡路里值,将其附加到该用户的相应卡路里列表中,然后将该列表附加到新列中。但是,新生成的列长度为25:
Current data: 21
New column: 25
有人知道为什么会这样吗?这是下面的代码,以及原始数据集和新列的示例:
while len(col) < len(data['post']):
for post,api_id,api_key in zip(data['post'],ids_keys.keys(),ids_keys.values()): # cycles through text data & api keys
headers = {
'Content-Type': 'application/x-www-form-urlencoded','x-app-id': api_id,'x-app-key': api_key,'x-remote-user-id': '0'
}
calories = []
print('Current data:',len(data['post']),'\n New column: ',len(col)) # prints length of post vs new cal column
for word in eval(post):
if word not in food:
continue
else:
print('Detected Word: ',word)
query = {'query': '{}'.format(word)}
try:
response = requests.request("POST",url,headers=headers,data=query)
except KeyError as ke:
print(ke,'Out of calls,next key...')
ids_keys.pop(api_id) # drop current api id & key from dict if out of calls
print('API keys left:',len(ids_keys))
finally:
stats = response.json()
print('Food Stats: \n',stats)
print('Calories in food: ',stats['foods'][0]['nf_calories'])
calories.append(stats['foods'][0]['nf_calories'])
print('Current Key',':',api_key)
col.append(calories)
if len(col) == len(data['post']):
break
我试图使用while循环仅追加到数据集的长度,但无济于事。
原始数据集:
pd.DataFrame({'user':['avskk','janejellyn','firlena227','...'],'date': ['October 22','October 22','post': [['autumn','fully','arrived','cooking','breakfast','toaster',['breakfast','chinese','sticky','rice','tempeh','sausage','cucumber','salad','lunch','going','coworkers','probably','black','bean','burger'],['potato','inspiring','food','day','today','...']]
})
新列:
pd.DataFrame({'Calories': [[22,33,45,32,2,5,7,9,76],[43,78,54,97,56,97],[23,55,22,99,66,98,35,33]]
})
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)