python pandas- AttributeError:'Series'对象没有属性'columns'?

问题描述:

我正在尝试计算特定列'df1'的当前行值落在前5行(2列并排)中的低 - 高范围值之间的次数。这是一个后续问题 - Dickster已经完成了繁重的工作herepython pandas- AttributeError:'Series'对象没有属性'columns'?

The()。之间()方法不合作,抱怨AttributeError: 'Series' object has no attribute 'columns'。我不明白我是如何涉及columns属性。

list1 = [[21,101],[22,110],[25,113],[24,112],[21,109],[28,108],[30,102],[26,106],[25,111],[24,110]] 
dict1 = {} 
dict1['df1'] = pd.DataFrame(list1,index=pd.date_range('2000-1-1',periods=10, freq='D'), columns=list('AB')) 
dict1['df2'] = pd.DataFrame(dict1['df1'] * (1-.05)) 
pan_so = pd.Panel(dict1) 
pan_so = pan_so.transpose(2,1,0) 

x = pan_so.ix[0,:,:] 
def btwn(x): # x is a dataframe 
    y = x['df1'].rolling(center=False,window=6) 
    z = x['df2'].rolling(center=False,window=6) 
    x['cnt_btwn'] = pd.Series(pd.Series(y[:-1]).between(z[-1], y[-1], inclusive=True).sum()) 
    return x 
btwn(x) 

我在做什么错?谢谢!

这个y[:-1]可以访问不支持列索引的Rolling对象,也就是代码中[:-1]的含义。您应该应用转换函数并在过滤之前获取实际系列。

+2

嗨布德 - 可能是一个更具体的转换函数? – MJS

+0

像平均最大计数等等,我不清楚为什么你把这样的[:-1]放在那里,前面的问题很难读取;我所知道的是,虽然y是一个滚动对象,而不是一个系列,所以你不能在指定的位置调用 – Boud

+0

我想将“当前”行数据与一个滚动窗口[curr-6: curr-1],所以[:-1]就是隔离当前行。谢谢你让我走上正确的道路。 – MJS