【发布时间】:2021-10-13 16:32:27
【问题描述】:
数据1:shanghaiData.csv
数据2:timesData.csv
我刚刚学习使用来自 CSV 和可视化的数据。我正在尝试合并两个 CSV 表,然后在特定大学的条形图上绘制两个世界排名。
我的代码是:
import pandas as pd
import matplotlib
times_df = pd.read_csv("timesData.csv")
shanghai_df = pd.read_csv("shanghaiData.csv")
combined = times_df.merge(shanghai_df, on='university_name', how='left')
combined = combined.loc[combined['university_name']=='Harvard University']
combined.plot(
kind = 'bar',
x = 'year_y',
y = ['world_rank_x', 'world_rank_y']
)
TypeError Traceback (most recent call last)
<ipython-input-1-e360afadaed4> in <module>
3 combined.loc[combined['university_name']=='Harvard University']
4 combined = combined.loc[combined['university_name']=='Harvard University']
----> 5 combined.plot(
6 kind = 'bar',
7 x = 'year_y',
/usr/lib/python3.9/site-packages/pandas/plotting/_core.py in __call__(self, *args, **kwargs)
970 data.columns = label_name
971
--> 972 return plot_backend.plot(data, kind=kind, **kwargs)
973
974 __call__.__doc__ = __doc__
/usr/lib/python3.9/site-packages/pandas/plotting/_matplotlib/__init__.py in plot(data, kind, **kwargs)
69 kwargs["ax"] = getattr(ax, "left_ax", ax)
70 plot_obj = PLOT_CLASSES[kind](data, **kwargs)
---> 71 plot_obj.generate()
72 plot_obj.draw()
73 return plot_obj.result
/usr/lib/python3.9/site-packages/pandas/plotting/_matplotlib/core.py in generate(self)
284 def generate(self):
285 self._args_adjust()
--> 286 self._compute_plot_data()
287 self._setup_subplots()
288 self._make_plot()
/usr/lib/python3.9/site-packages/pandas/plotting/_matplotlib/core.py in _compute_plot_data(self)
451 # no non-numeric frames or series allowed
452 if is_empty:
--> 453 raise TypeError("no numeric data to plot")
454
455 self.data = numeric_data.apply(self._convert_to_ndarray)
TypeError: no numeric data to plot
如果我尝试将年份转换为整数,我仍然会收到错误。
combined['year_y'] = combined['year_y'].astype(int)
【问题讨论】:
标签: python pandas csv matplotlib merge