是否可以通过点表示法而不是括号表示法访问系列项目?
s = pandas.Series(dict(a=4, b=4))
print s['a'] # works
print s.a # fails
我们可以使用DataFrame:
df = pandas.DataFrame([dict(a=4, b=4), dict(a=4, b=4)])
print df['a'] # works
print df.a # works
解决方法:
我通过重载Series .__ get_attr__方法获得行为:
def my__getattr__(self, key):
# If attribute is in the self Series instance ...
if key in self:
# ... return is as an attribute
return self[key]
else:
# ... raise the usual exception
raise AttributeError("'Series' object has no attribute '%s'" % key)
# Overwrite current Series attributes 'else' case
pandas.Series.__getattr__ = my__getattr__
然后我可以使用属性访问Seriee项目:
xx = pandas.Series(dict(a=44, b=55))
xx.a
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。