无法使用我的数据框绘制条形图

时间:2021-03-05 23:15:54

标签: python dataframe matplotlib

我正在尝试将以下数据框绘制成条形图:

        JP_genre JP_sales
0         Action   159.95
1      Adventure    52.07
2       Fighting    87.35
3           Misc   107.76
4       Platform   130.77
5         Puzzle    57.31
6         Racing    56.69
7   Role-Playing   352.31
8        Shooter    38.28
9     Simulation    63.70
10        Sports   135.37
11      Strategy    49.46

当我输入以下代码时:

JP_df.plot(x = 'JP_genre', y = 'JP_sales', kind= 'bar')

我收到错误

TypeError: no numeric data to plot

知道为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

您的代码没有错误。这是我尝试过的:

import pandas as pd
import matplotlib.pyplot as plt

JP_df = pd.read_csv("temp.csv")
print(JP_df)
JP_df.plot(x='JP_genre', y='JP_sales', kind='bar')
plt.show()

将您上面粘贴的数据保存到 temp.csv 后,运行完美。

您可能出错的是 DataFrame 中 JP_sales 列的值的类型不是数值(即它们可以保存为字符串)。 您可以在调用 .plot() 之前尝试将第二列转换为浮点数,如下所示:

JP_df = JP_df.astype({"JP_genre": str, "JP_sales": float})
JP_df.plot(x='JP_genre', y='JP_sales', kind='bar')
plt.show()
相关问题