ERROR:
[pivot table] : 'some' is both an index and a column label.
SOLVE:
pandas.Series.droplevel
total_births_advance = top_1000_plot.pivot_table('births', index='year', columns='name', aggfunc=sum)
[year]同时是index和column,出现矛盾。
打印top_1000,长这样:
复制副本,删掉最左侧的index[year]:
# form a pivot_table #
top_1000_plot = top_1000.droplevel('year') # remove index
# print(top_1000_plot)
total_births_advance = top_1000.pivot_table('births', index='year', columns='name', aggfunc=sum)
# print(total_births_advance)
subset = total_births_advance[['John', 'Harry', 'Mary', 'Marilyn']]
subset.plot(subplots=True, figsize=(12, 10), grid=False, title='Number of births per year')
plt.show()
副本长这样:
顺利出图: