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

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.csdn.net/qq_36448051/article/details/81592379
https://blog.csdn.net/weixin_39777626/article/details/78760076
https://cloud.tencent.com/developer/ask/153695/answer/266131

你可能感兴趣的:(Python,笔记)