使用多列数据绘制折线图 (pandas)

Draw a line chart using multiple columns data (pandas)

我正在尝试根据以下数据绘制折线图。在 x 轴上,我想显示年份,在 y 轴上,我想显示不同 'Borough' 的人口。基本上,我想绘制这些年来所有行政区的人口。我编写了以下代码来转置数据并绘制折线图。我收到错误 - “没有要绘制的数字数据”。第一张图是转置数据,第二张图是原始数据

bar_plot = bar_plot.transpose()
bar_plot
bar_plot.columns = ['NYC Total Population', 'Bronx Population', 'Brooklyn Population'   , 'Manhattan Population',   'Queens Population',    'Staten Island Population']
bar_plot.drop('Borough')
bar_plot.plot(kind = 'line', y = ['NYC Total Population',   'Bronx Population', 'Brooklyn Population'   , 'Manhattan Population',   'Queens Population',    'Staten Island Population'])

您的剧情代码没有错误。 如果数据类型是类别,则会发生此错误。注意使用 for ex.:

转换和验证数字类型
bar_plot = bar_plot.apply(pd.to_numeric)
bar_plot.dtypes

下面的简化代码给出了一个线图:

import pandas as pd

bar_plot = pd.DataFrame({'NYC Total Population': [200, 400, 600], 'Staten Island': [30, 60, 90]}, index=[1950, 1960, 1970])

bar_plot.plot(kind = 'line', y = ['NYC Total Population', 'Staten Island'])