pandas 熊猫系列'对象没有属性'find'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45913432/
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 04:20:17  来源:igfitidea点击:

pandas Series' object has no attribute 'find'

pythonpandasnumpymatplotlibjupyter-notebook

提问by Pbch

I am trying to do simple plot of data and getting the following error.. any help is very much appreciated

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

AttributeError:'Series' object has no attribute 'find'

AttributeError:'Series' 对象没有属性 'find'

Versions :python3 , matplotlib (2.0.2) , pandas (0.20.3) , jupyter (1.0.0).

版本:python3、matplotlib (2.0.2)、pandas (0.20.3)、jupyter (1.0.0)。

Code:

代码:

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()

Out Put / Error:

输出/错误:

    -
    ——

--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in () 9 #print(type(y[0])) 10 ---> 11 plt.plot(x,y) 12 plt.show()

       /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/pyplot.py
   in plot(*args, **kwargs)
          3315                       mplDeprecation)
          3316     try:
       -> 3317         ret = ax.plot(*args, **kwargs)
          3318     finally:
          3319         ax._hold = washold

       /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/__init__.py
   in inner(ax, *args, **kwargs)
          1896                     warnings.warn(msg % (label_namer, func.__name__),
          1897                                   RuntimeWarning, stacklevel=2)
       -> 1898             return func(ax, *args, **kwargs)
          1899         pre_doc = inner.__doc__
          1900         if pre_doc is None:

       /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/axes/_axes.py
   in plot(self, *args, **kwargs)
          1404         kwargs = cbook.normalize_kwargs(kwargs, _alias_map)
          1405 
       -> 1406         for line in self._get_lines(*args, **kwargs):
          1407             self.add_line(line)
          1408             lines.append(line)

       /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/axes/_base.py
   in _grab_next_args(self, *args, **kwargs)
           405                 return
           406             if len(remaining) <= 3:
       --> 407                 for seg in self._plot_args(remaining, kwargs):
           408                     yield seg
           409                 return

       /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/axes/_base.py
   in _plot_args(self, tup, kwargs)
           355         ret = []
           356         if len(tup) > 1 and is_string_like(tup[-1]):
       --> 357             linestyle, marker, color = _process_plot_format(tup[-1])
           358             tup = tup[:-1]
           359         elif len(tup) == 3:

       /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/axes/_base.py
   in _process_plot_format(fmt)
            92     # handle the multi char special cases and strip them from the
            93     # string
       ---> 94     if fmt.find('--') >= 0:
            95         linestyle = '--'
            96         fmt = fmt.replace('--', '')

       /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/generic.py
   in __getattr__(self, name)
          3079             if name in self._info_axis:
          3080                 return self[name]
       -> 3081             return object.__getattribute__(self, name)
          3082 
          3083     def __setattr__(self, name, value):

       AttributeError: 'Series' object has no attribute 'find'

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

       /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/pyplot.py
   in plot(*args, **kwargs)
          3315                       mplDeprecation)
          3316     try:
       -> 3317         ret = ax.plot(*args, **kwargs)
          3318     finally:
          3319         ax._hold = washold

       /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/__init__.py
   in inner(ax, *args, **kwargs)
          1896                     warnings.warn(msg % (label_namer, func.__name__),
          1897                                   RuntimeWarning, stacklevel=2)
       -> 1898             return func(ax, *args, **kwargs)
          1899         pre_doc = inner.__doc__
          1900         if pre_doc is None:

       /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/axes/_axes.py
   in plot(self, *args, **kwargs)
          1404         kwargs = cbook.normalize_kwargs(kwargs, _alias_map)
          1405 
       -> 1406         for line in self._get_lines(*args, **kwargs):
          1407             self.add_line(line)
          1408             lines.append(line)

       /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/axes/_base.py
   in _grab_next_args(self, *args, **kwargs)
           405                 return
           406             if len(remaining) <= 3:
       --> 407                 for seg in self._plot_args(remaining, kwargs):
           408                     yield seg
           409                 return

       /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/axes/_base.py
   in _plot_args(self, tup, kwargs)
           355         ret = []
           356         if len(tup) > 1 and is_string_like(tup[-1]):
       --> 357             linestyle, marker, color = _process_plot_format(tup[-1])
           358             tup = tup[:-1]
           359         elif len(tup) == 3:

       /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/axes/_base.py
   in _process_plot_format(fmt)
            92     # handle the multi char special cases and strip them from the
            93     # string
       ---> 94     if fmt.find('--') >= 0:
            95         linestyle = '--'
            96         fmt = fmt.replace('--', '')

       /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/generic.py
   in __getattr__(self, name)
          3079             if name in self._info_axis:
          3080                 return self[name]
       -> 3081             return object.__getattribute__(self, name)
          3082 
          3083     def __setattr__(self, name, value):

       AttributeError: 'Series' object has no attribute 'find'

采纳答案by Pbch

Following worked for me and hope it helps.... Issue was mixing differnt data types for plotting.

以下对我来说有效,希望它有所帮助.... 问题是混合不同的数据类型进行绘图。

    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

在此处输入图片说明

回答by jezrael

I think you can use DataFrame.plotwith define xand yby columns names, because it better support plotting non numeric values:

我认为您可以使用DataFrame.plotwith 定义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()

Sample:

样本:

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

图形

But if all values are numeric it works nice:

但是如果所有值都是数字,它就很好用:

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()