python pandas-AttributeError: 'Series' 对象没有属性 'columns'? [英] python pandas- AttributeError: 'Series' object has no attribute 'columns'?

查看:310
本文介绍了python pandas-AttributeError: 'Series' 对象没有属性 'columns'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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) 

我做错了什么?谢谢!

推荐答案

这个 y[:-1] 可以访问不支持的 Rolling 对象列索引,这就是代码中 [:-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.

这篇关于python pandas-AttributeError: 'Series' 对象没有属性 'columns'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆