2012-09-12 40 views
4

是否可以通過點符號而不是括號表示訪問系列項目?訪問帶點符號的大熊貓系列項目,如DataFrame

s = pandas.Series(dict(a=4, b=4)) 
print s['a'] # works 
print s.a  # fails 

我們可以用數據幀做:

df = pandas.DataFrame([dict(a=4, b=4), dict(a=4, b=4)]) 
print df['a'] # works 
print df.a  # works 

回答

3

我通過重載系列.__ 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 
-1

不可能的。您可以將該系列轉換爲一列DataFrame。

+0

當然,是不是很_processing_有效! – PhE

+1

我會考慮添加該功能:http://github.com/pydata/pandas/issues/1903 –