我不明白如何在图表上绘制xticks。我想将数据框的“运动”列用作我的xticks值。
数据在图像中。
div1['Athletes'] = pd.to_numeric(div1.Athletes.str.replace(',', ''))
ax = div1[['Sport','Athletes']].plot(kind='bar', title="Number of Athletes in Men's Individual Sports in NCAA Division 1", figsize=(15, 10), legend=True, fontsize=12)
SportList = div1['Sport'].tolist()
ax.set_xticklabels(SportList)
错误:
AttributeError Traceback (most recent call last)
<ipython-input-44-4b8025d176c6> in <module>()
----> 1 div1['Athletes'] = pd.to_numeric(div1.Athletes.str.replace(',', ''))
2 ax = div1[['Sport','Athletes']].plot(kind='bar', title="Number of Athletes in Men's Individual Sports in NCAA Division 1", figsize=(15, 10), legend=True, fontsize=12)
3 SportList = div1['Sport'].tolist()
4 ax.set_xticklabels(SportList)
/opt/conda/lib/python3.5/site-packages/pandas/core/generic.py in
__getattr__(self, name)
2738 if (name in self._internal_names_set or name in self._metadata or
2739 name in self._accessors):
-> 2740 return object.__getattribute__(self, name)
2741 else:
2742 if name in self._info_axis:
/opt/conda/lib/python3.5/site-packages/pandas/core/base.py in __get__(self, instance, owner)
239 # this ensures that Series.str.<method> is well defined
240 return self.accessor_cls
--> 241 return self.construct_accessor(instance)
242
243 def __set__(self, instance, value):
/opt/conda/lib/python3.5/site-packages/pandas/core/strings.py in
_make_str_accessor(self)
1839 # (instead of test for object dtype), but that isn't practical for
1840 # performance reasons until we have a str dtype (GH 9343)
-> 1841 raise AttributeError("Can only use .str accessor with string "
1842 "values, which use np.object_ dtype in "
1843 "pandas")
AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas
答案 0 :(得分:0)
您的Athletes
系列包含的类型不是不是字符串。如果确定其中某些值是字符串,则可以在使用str
访问器之前先转换为.str
:
div1['Athletes'] = pd.to_numeric(div1['Athletes'].astype(str).str.replace(',', ''))
答案 1 :(得分:0)
在使用.plot
方法之前,将DataFrame
索引设置为'Sport',大熊猫将处理其余部分:
ax = div1[['Sport','Athletes']].set_index('Sport').plot(kind='bar', title="Number of Athletes in Men's Individual Sports in NCAA Division 1", figsize=(15, 10), legend=True, fontsize=12, rot=0)