[英]Multiline plot for each id
我想在 Python 中為此數據集繪制多行:(x = 年,y = 頻率)
Student_ID Year Freq
A 2012 6
B 2008 22
C 2009 18
A 2010 7
B 2012 13
D 2012 31
D 2013 1
其中每個 student_id 都有不同年份的數據。 count 是 groupby 的結果。 我希望每個 student_id 都有一行。
我試過這個:
df.groupby(['year'])['freq'].count().plot()
但它不會為每個 student_id 繪制一行。
任何建議都非常受歡迎。 感謝您的幫助
從你的問題中我不確定你是想要count
(在你的例子中是所有的)還是sum
,所以我用sum
解決了這個問題 - 如果你想要count
,只需在第一行將它換成sum
。
df_ = df.groupby(['Student_ID', 'Year'])['Freq'].sum()
print(df_)
> Student_ID Year
A 2010 7
2012 6
B 2008 22
2012 13
C 2009 18
D 2012 31
2013 1
Name: Freq, dtype: int64
fig, ax = plt.subplots()
for student in set(a[0] for a in df_.index):
df_[student].plot(ax=ax, label=student)
plt.legend()
plt.show()
這給了你:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.