如何在图形上对值进行排序

时间:2020-02-19 10:59:48

标签: python pandas matplotlib

我正在使用python分析 911服务呼叫数据集。我正在按月显示数据。数据未按日期排序。

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

1 个答案:

答案 0 :(得分:1)

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