试图制作条形图时发生“ TypeError:'DataFrame'对象不可调用”

时间:2020-06-23 17:53:49

标签: python pandas dataframe typeerror

TypeError:“ DataFrame”对象不可调用

data2('New_deaths', 'New_cases').head().plot.bar(title= "Statistics of 
New_deaths and New_cases by country")#bar graph

2 个答案:

答案 0 :(得分:0)

这行吗?

data2['New_deaths', 'New_cases'].head().plot.bar(title= "Statistics of New_deaths and New_cases by country")#bar graph

通过这种方式,您不会尝试调用数据框,而是在访问它。

答案 1 :(得分:0)

您的问题不清楚。因此,我根据您想做的事情给出答案。 要绘制一列数据框,代码将如下所示:

data2["New_deaths"].head().plot.bar(title= "Statistics of New_deaths and New_cases by country")

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
x = data2["New_deaths"].drop_duplicates()
graph = ax.bar(x, data = data2["New_deaths"].head(), height= data2["New_deaths"].max() + 100, label="data")
ax.set_title("Statistics of New_deaths and New_cases by country")
plt.show()

如果您尝试同时绘制数据框的两列(如并排条),则可以签出以下内容:https://matplotlib.org/3.2.1/gallery/lines_bars_and_markers/barchart.html#sphx-glr-gallery-lines-bars-and-markers-barchart-py

如果您要进行分组方式:

data2.groupby(["New_deaths","New_cases"]).head().plot.bar(title= "Statistics of New_deaths and New_cases by country")

pandas groupby的链接:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.groupby.html