我是Python和StackOverflow的新手。希望我能正确发布:)
问题:在每个for循环中,我都试图创建一个新的数据框,向其中添加列,并用数据填充列。
方法:通过阅读以前的文章,我了解到可以使用“ exec”创建新的数据框,但这不是最佳实践。或者,我可以创建一个dic,然后修改df。一直在尝试将嵌入式用于循环
问题:实际上是修改并填写已创建(且为空)的df
代码:
if (to.matched.some(record => record.meta.requiresAuth)) {
if (!store.getters.isLoggedIn) {
next({
name: 'login'
})
} else {
// then check if loged user "type" is admin (any other possebilities are denied)
if (!store.getters.loggedUser.type === 'admin' || store.getters.loggedUser.type === '' || store.getters.loggedUser.type === null || store.getters.loggedUser.type === undefined || store.getters.loggedUser.type === 'worker') {
next({
name: 'dashboard'
})
} else {
next()
}
}
}
else {
next()
}
由于方法1不在循环中,因此它无济于事。
df1 = an existing dataframe with date as its index and profit,tax,and revenue for columns
#tablelist will only have these 4 values
tablelist = ['QTD','YTD','OneYear','Inception']
# Method 1
for table in tablelist:
#this works
exec('{} = pd.DataFrame()'.format(table))
#this doesn't work
table['Value'] = df1[profit] - df1[tax]
---> error: 'str' object does not support item assignment
# Output (without line 4)
type(YTD)
pandas.core.frame.DataFrame
#this works but its outside of the loop so it defeats purpose
YTD['Value'] = df1[profit] - df1[tax]
方法2有效,但是如何添加列和数据?
任何建议都将不胜感激!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。