首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError: 4.3不是字符串

TypeError: 4.3不是字符串
EN

Stack Overflow用户
提问于 2020-05-08 03:03:30
回答 1查看 87关注 0票数 0

在执行以下代码时,我得到了以下错误:

代码语言:javascript
复制
import pandas as pd
reviews=pd.read_csv("/kaggle/input/fivethirtyeight-fandango-dataset/fandango_score_comparison.csv")
print(reviews.head())
cols=['FILM','RT_user_norm','Metacritic_User','IMDB_norm','Fandango_Ratingvalue','Fandango_Stars']
norm_reviews=reviews[cols]
#print(norm_reviews)
print(norm_reviews[0:1])
import matplotlib. pyplot as plt
from numpy import arange
num_cols=['FILM','RT_user_norm','Metacritic_User','IMDB_norm','Fandango_Ratingvalue','Fandango_Stars']
bar_heights=norm_reviews.loc[0,num_cols].values
print(help(bar_heights))
print(bar_heights)
bar_positions=arange(5)+0.75
fig, ax= plt.subplots()
ax.bar(bar_positions,bar_heights,0.3)
plt.show()

下面是错误:

代码语言:javascript
复制
/opt/conda/lib/python3.7/site-packages/matplotlib/category.py in __init__(self, data)
    179         self._counter = itertools.count()
    180         if data is not None:
--> 181             self.update(data)
    182 
    183     @staticmethod

/opt/conda/lib/python3.7/site-packages/matplotlib/category.py in update(self, data)
    217             # OrderedDict just iterates over unique values in data.
    218             if not isinstance(val, (str, bytes)):
--> 219                 raise TypeError("{val!r} is not a string".format(val=val))
    220             if convertible:
    221                 # this will only be called so long as convertible is True.

TypeError: 4.3 is not a string

在行bar_heights=norm_reviews.loc[0,num_cols].values之前是bar_heights=norm_reviews.ix[0,num_cols].values,这又给出了另一个错误。将$ix$更改为$loc$后,出现字符串错误

EN

回答 1

Stack Overflow用户

发布于 2020-05-09 03:15:27

我确信有一种更好的方法来格式化它,但这在FWIW中有效。

代码语言:javascript
复制
import pandas as pd
import matplotlib. pyplot as plt
from numpy import arange

plt.style.use('seaborn')

review = pd.read_csv("https://raw.githubusercontent.com/fivethirtyeight/data/master/fandango/fandango_score_comparison.csv")

review_filtered  = review.filter(['FILM','RT_user_norm','Metacritic_User','IMDB_norm','Fandango_Ratingvalue','Fandango_Stars'])

col_names = ['Movie', 'RT User', 'Metacritic User', 'IMDB Score', 'Fandango Rating', 'Fandango Stars']

review_filtered.plot(kind = 'bar', xticks= [], width = 0.3, legend='upper right')
plt.show

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61665751

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档