4

角かっこ表記の代わりにドット表記でシリーズアイテムにアクセスすることはできますか?

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
4

2 に答える 2

3

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
于 2012-09-12T08:55:04.140 に答える
-1

ありえない。シリーズを1列のDataFrameに変換できます。

于 2012-09-12T08:30:24.320 に答える