熊猫系列'对象没有属性'找到'

时间:2017-08-28 07:04:34

标签: python pandas numpy matplotlib jupyter-notebook

我正在尝试做简单的数据绘图并得到以下错误..非常感谢任何帮助

属性错误:'系列'对象没有属性'找到'

版本 python3, matplotlib(2.0.2), 大熊猫(0.20.3), jupyter(1.0.0)。

代码:

setTimeout

输出/错误:

      -
  

                           -------------------------------------------------- -------------------------              AttributeError Traceback(最近一次调用最后一次)               in()                    9 #print(类型(y [0]))                   10              ---> 11 plt.plot(x,y)                   12 plt.show()

import pandas as pd
import matplotlib.pyplot as plt
pd_hr_data = pd.read_csv("/Users/pc/Downloads/HR_comma_sep.csv")

#print(pd_hr_data['average_montly_hours'],pd_hr_data['sales'])
take_ten_data = pd_hr_data[0:19]
x = take_ten_data['average_montly_hours'].astype(int)
y = take_ten_data['sales'].astype(str)
print(type(x[0]))
print(type(y[0]))
#print(x,y) ---- this gives me all the 20 values
#print(type(y[0]))
plt.plot(x,y)
plt.show()

2 个答案:

答案 0 :(得分:1)

我认为您可以使用DataFrame.plot定义xy列名称,因为它更好地支持绘制非数字值:

take_ten_data = pd_hr_data[0:19]
x = take_ten_data['average_montly_hours'].astype(int)
y = take_ten_data['sales'].astype(str)

take_ten_data.plot(x='average_montly_hours', y='sales')
#working without x,y also, but less readable
#take_ten_data.plot('average_montly_hours','sales')
plt.show()

样品:

take_ten_data = pd.DataFrame({'average_montly_hours':[3,10,12], 'sales':[10,20,30]})
x = take_ten_data['average_montly_hours'].astype(int)
y = take_ten_data['sales'].astype(str)

take_ten_data.plot(x='average_montly_hours', y='sales')
plt.show()

graph

但如果所有值都是数字,那么效果很好:

take_ten_data = pd.DataFrame({'average_montly_hours':[3,10,12], 'sales':['10','20','30']})

x = take_ten_data['average_montly_hours'].astype(int)
#convert to int if necessary
y = take_ten_data['sales'].astype(int)

plt.plot(x,y)
plt.show()

答案 1 :(得分:0)

以下为我工作并希望它有所帮助....问题是混合不同的数据类型进行绘图。

    import pandas as pd
    import matplotlib.pyplot as plt
    pd_hr_data = pd.read_csv("/Users/pc/Downloads/HR_comma_sep.csv")
    take_ten_data = pd_hr_data[0:4]
    y = take_ten_data['average_montly_hours'].astype(int)
    x = [1,2,3,4] ----this is can be autogenerated based on the series/matrix size
    names = take_ten_data['sales']
    plt.bar(x,y, align='center')
    #plt.plot(x,y) ---- use this if you want 
    plt.xticks(x, names)
    plt.show()

enter image description here