简体   繁体   中英

Plotting count of unique values in groupby

I have a dataset with that form:

>>> df
         my_timestamp    disease  month
0 2016-01-01 15:00:00       2      jan
0 2016-01-01 11:00:00       1      jan
1 2016-01-02 15:00:00       3      jan  
2 2016-01-03 15:00:00       4      jan  
3 2016-01-04 15:00:00       2      jan  
  

I wont to count the number of unique apparition by month, by values, then plot the count of every value by month.

df values count
jan 2 3 jan 2 3

How can I plot it? In one plot with month on x axis, one line for every values, and their count on y

If you want to plot by month, then you also need to plot by year if multiple years. You can use dt.strftime when using .groupby to group by year and month.

Given the following slightly altered dataset to include more months:

       my_timestamp  disease    month
2016-01-01 15:00:00       2      jan
2016-02-01 11:00:00       1      feb
2017-01-02 15:00:00       3      jan  
2017-01-02 15:00:00       4      jan  
2016-01-04 15:00:00       2      jan  

You can run the following

df['my_timestamp'] = pd.to_datetime(df['my_timestamp'])
df.groupby(df['my_timestamp'].dt.strftime('%Y-%m'))['disease'].nunique().plot()

在此处输入图像描述

What I did to get that data into barplot. I created a month column. Then:

for v in df.disease.unique():
   diseases = df_cut[df_cut['disease']==v].groupby('month_num')['disease'].count()
   x = diseases.index
   y = diseases.values
   plt.bar(x, y)
  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM