此代码中的问题是在一年而不是所有年份中显示,您能帮我修复代码以显示所有年份吗?
ks['Year'] = pd.to_datetime(ks['Deadline']).dt.year
unique_categories = ks['Category'].unique()
for category in unique_categories:
product_df = ks[ks['Category'] == category]
product_df = product_df.head(12)
product_counts = product_df.groupby('Year').size()
plt.plot(product_counts.index, product_counts.values, marker='o', linestyle='-', label=category)
plt.xlabel('Year')
plt.ylabel('Count')
plt.title(f'Count of {category} Category Over the Years (First 12 Entries)')
plt.xticks(product_counts.index, rotation=45, ha='right')
plt.legend()
plt.show()
在此处输入图像描述如何解决此问题?
检查product_df 中的内容。 由于只需要前 12 个,因此可能只有 1 个独特的年份