'Please how can i plot a line graph on this data frame, I want to show the sum of attendance on a monthly basis in line graph
| visit_date | sex | age | marital_status |
|---|---|---|---|
| 0 2021-04-05 | Female | 66 | Widowed |
| 1 2021-04-05 | Female | 65 | Widowed |
| 2 2021-04-05 | Female | 39 | Married |
| 3 2021-04-05 | Male | 56 | Married |
| 4 2021-04-05 | Female | 31 | Married |
| 5 2021-04-05 | Female | 24 | Single |
| 6 2021-04-05 | Male | 37 | Married |
| 7 2021-04-05 | Female | 43 | Widowed |
| 8 2021-04-05 | Male | 29 | Married |
| 9 2021-04-05 | Male | 44 | Single |
Solution 1:[1]
If I understood correctly this should work
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(data)
df['visit_date'] = pd.to_datetime(df['visit_date'])
attendance = df.groupby(pd.Grouper(key="visit_date", freq='M')).size().reset_index(name='attendance')
attendance.plot(x='visit_date', y='attendance', kind='line')
plt.show()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Isra |
