python pandas- AttributeError: 'Series' object has no attribute 'columns'?

懵懂的女人 提交于 2020-01-03 17:27:06

问题


I am trying to count the number of times the current row's value for a specific column 'df1' falls between the low-high range values in the previous 5 rows (in 2 side by side columns). This is a follow-up question - Dickster has already done the heavy lifting here.

The Series().between() method is not cooperating, complaining that AttributeError: 'Series' object has no attribute 'columns'. I don't understand how I am involving the columns attribute.

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) 

What am I doing wrong? Thanks!


回答1:


This y[:-1] makes an access to a Rolling object that doesn't support column indexing, that is the meaning of [:-1] in your code. You should apply a transformation function and get an actual series before filtering.



来源:https://stackoverflow.com/questions/40366120/python-pandas-attributeerror-series-object-has-no-attribute-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!