Diego Sisalima
Diego Sisalima

Reputation: 13

Plot Dataframe by DataTime Grouping by Month, Year, Day

How can I make a Plot counting the elements that are in a specific Time Group. I have a dataframe like this

Incident_Number Submit_Date Description
001 05/04/2017 12:00:45 Problem1
002 05/05/2017 13:00:00 Problem2
003 05/05/2017 14:00:00 Problem3
004 07/05/2017 19:00:00 Problem4
005 07/06/2017 08:00:00 Problem5

and how could be possible to make a line plot that show me the total Incidents by month, date, weekday, or year. I tried grouping by, but this take many lines, first extracting the month, year, and date and then transforming again in datetime to visualize. Any ideas?

Thanks for your help

Upvotes: 1

Views: 5064

Answers (1)

DYZ
DYZ

Reputation: 57033

Start with converting Submit_Date into a timedate (if it is not a timedate yet) and making it the index:

df['Submit_Date'] = pd.to_datetime(df['Submit_Date'])
df.set_index('Submit_Date', inplace = True)

Now you can resample your data at any frequency and plot it. For example, resample by 1 month (get monthly counts):

df.resample('1M').count()['Description'].plot()

Upvotes: 3

Related Questions