简体   繁体   中英

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

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM