Techie Boy
Techie Boy

Reputation: 159

How to Sort Values on the Graph

I'm using python to analyze 911 Call for Service dataset. I'm showing data monthwise. Data is not sorted Date Wise.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('911_calls_for_service.csv')
r, c = df.shape

df['callDateTime'] = pd.to_datetime(df['callDateTime'])
df['MonthYear'] = df['callDateTime'].apply(lambda time: str(time.year) + '-' + str(time.month))
df['MonthYear'].value_counts().plot()
print(df['MonthYear'].value_counts())
plt.tight_layout()
plt.show()

enter image description here

Upvotes: 0

Views: 50

Answers (1)

Agent Smith
Agent Smith

Reputation: 146

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('911_calls_for_service.csv')
df['callDateTime'] = pd.to_datetime(df['callDateTime'])

ax = df['callDateTime'].groupby([df["callDateTime"].dt.year, df["callDateTime"].dt.month]).count().plot()
ax.set_xlabel("Date")
ax.set_ylabel("Frequency")

plt.tight_layout()
plt.show()

enter image description here

Upvotes: 1

Related Questions