admin管理员组文章数量:1400533
I have a very simple for loop which was working last week but when I run it this week on exactly the same data it is not working... when I run the code outside of the loop it works without an error. I don't understand...
The code is: '''
survey = pd.read_csv(folderin + 'survey.csv')
CSsurvey = pd.read_csv(folderin + 'CS_survey.csv')
sports = ['Value MTB related (e.g. inner tubes, water bottles etc)',
'Value Running','Value Roaming and other outdoor related (e.g. climbing, kayaking)',
'Value Outdoor sports event related (e.g.race)']
sport = []
for p in sports:
item = survey[p].sum()
CSitem = CSsurvey[p].sum()
total = item + CSitem
sport.append(total)'''
I get the error:
If I run the code for each 'p' outside of the loop I don't get an error
I have a very simple for loop which was working last week but when I run it this week on exactly the same data it is not working... when I run the code outside of the loop it works without an error. I don't understand...
The code is: '''
survey = pd.read_csv(folderin + 'survey.csv')
CSsurvey = pd.read_csv(folderin + 'CS_survey.csv')
sports = ['Value MTB related (e.g. inner tubes, water bottles etc)',
'Value Running','Value Roaming and other outdoor related (e.g. climbing, kayaking)',
'Value Outdoor sports event related (e.g.race)']
sport = []
for p in sports:
item = survey[p].sum()
CSitem = CSsurvey[p].sum()
total = item + CSitem
sport.append(total)'''
I get the error:
If I run the code for each 'p' outside of the loop I don't get an error
Share Improve this question asked Mar 24 at 15:33 Heather KayHeather Kay 931 silver badge8 bronze badges 5 |1 Answer
Reset to default 1IIUC then you can rewrite the above for loop using pd.concat
and then taking the sum
over all columns and using column filtering to return on the sports columns finally add to_list
to convert those sums to a list.
Try:
sport = pd.concat([survey, CSurvey]).sum(axis=0)[sports].to_list()
本文标签:
版权声明:本文标题:pandas - return self._engine.get_loc(casted_key) error when in for loop, same code runs with no error outside of the loop - Stac 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744243695a2596908.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
sport = pd.concat([survey, CSurvey]).sum(axis=0)[sports].to_list()
– Scott Boston Commented Mar 24 at 20:27