首页 > 解决方案 > 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$ 后出现字符串错误

标签: pandas

解决方案


我确信有更好的格式化方法,但这适用于 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


推荐阅读