pandas - TypeError: 4.3 不是字符串
问题描述
执行以下操作时出现以下错误:
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()
这是错误:
/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
了另一个错误。BUt 将 $ix$ 改为 $loc$ 后出现字符串错误
解决方案
我确信有更好的格式化方法,但这适用于 FWIW。
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
推荐阅读
- ruby-on-rails - 你把 ActiveRecord 回调类放在哪里?
- asp.net-core - 使用 ASP.NET Core 2 身份验证时如何获取 LoginPath?
- ios - 在objectiveC中获取和设置NSUserActivity
- javascript - .toLowerCase() of undefined 在后台静默失败 - 但代码仍然可以正常工作
- string - Netlogo:如何从文本字符串中制作单个字符的列表?
- excel - Excel VBA:如何返回给定单元格的列号
- c++ - C++ Linux 用 char[14] 绑定套接字
- javascript - 为什么不能在反应钩子组件中设置间隔 id?
- python - 想返回四个选项,没有任何线索
- reactjs - 即使 Store 已成功更新,也无法显示来自 Redux Store 的值