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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40366120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:19:55  来源:igfitidea点击:

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

pythonpandasuser-defined-functionsseries

提问by MJS

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.

我正在尝试计算特定列 'df1' 的当前行值落在前 5 行(2 个并排列)中的低-高范围值之间的次数。这是一个后续问题 - Dickster 已经在这里完成了繁重的工作。

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 columnsattribute.

Series().between() 方法不合作,抱怨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) 

What am I doing wrong? Thanks!

我究竟做错了什么?谢谢!

采纳答案by Boud

This y[:-1]makes an access to a Rollingobject 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.

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