Pandas属性错误:AttributeError: 'Series' object has no attribute 'reshape' 解决办法

本文针对Python中使用pandas库时遇到的AttributeError,详细解释了如何解决'Series'对象没有'reshape'属性的问题,同时修复了DataFrame对象的排序及pandas模块的滚动平均函数调用错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AttributeError: ‘Series’ object has no attribute ‘reshape’ 解决办法

Python 3

w = [1.0i/k for i in range(k+1)]
w = data.describe(percentiles=w)[4:4+k+1]
w[0] = w[0]
(1-1e-10)
d2 = pd.cut(data, w, labels=range(k))
kmodel = KMeans(n_clusters=k, n_jobs=4)
kmodel.fit(data.reshape((len(data), 1)))
c = pd.DataFrame(kmodel.cluster_centers_).sort(0)
w = pd.rolling_mean(c, 2).iloc[1:]
w = [0]+list(w[0])+[data.max()]

上述代码,加粗语句会出现错误:

第一句:AttributeError: ‘Series’ object has no attribute ‘reshape’
第二句:AttributeError: ‘DataFrame’ object has no attribute ‘sort’
第三句:AttributeError: module ‘pandas’ has no attribute ‘rolling_mean’

解决办法:

  1. 用values方法将Series对象转化成numpy的ndarray,再用ndarray的reshape方法。改为: kmodel.fit(data.values.reshape((len(data), 1)))
  2. 将sort方法改为sort_values方法。
  3. 应该使用pandas.DataFrame.rolling然后应用mean(),应改为:w = c.rolling(2).mean().iloc[1:]

参考文章:
https://blog.youkuaiyun.com/qq_36448051/article/details/81592379
https://blog.youkuaiyun.com/weixin_39777626/article/details/78760076
https://cloud.tencent.com/developer/ask/153695/answer/266131

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值