pandas Series' 对象没有属性 'find'
Posted
技术标签:
【中文标题】pandas Series\' 对象没有属性 \'find\'【英文标题】:pandas Series' object has no attribute 'find'pandas Series' 对象没有属性 'find' 【发布时间】:2018-02-05 09:28:31 【问题描述】:我正在尝试绘制简单的数据图并收到以下错误。非常感谢任何帮助
AttributeError:“系列”对象没有“查找”属性
版本: 蟒蛇3, matplotlib (2.0.2) , 熊猫(0.20.3), jupyter (1.0.0)。
代码:
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()
输出/错误:
--------------------------------------------------- ------------------------- AttributeError Traceback(最近一次调用最后一次) 在 () 9 #print(类型(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'
【问题讨论】:
【参考方案1】:我认为您可以使用 DataFrame.plot
来定义 x
和 y
通过列名,因为它更好地支持绘制非数值:
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()
但如果所有值都是数字,它会很好用:
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()
【讨论】:
所以这里不需要x
和y
变量。【参考方案2】:
以下对我有用,希望对您有所帮助....问题是混合不同的数据类型进行绘图。
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()
【讨论】:
以上是关于pandas Series' 对象没有属性 'find'的主要内容,如果未能解决你的问题,请参考以下文章