我已经生成了示例输入数据(3个web站点,时间跨度:2年,100000个条目),因为您没有提供它。在
代码:import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('avito_trend.csv', parse_dates=['ts'], usecols=['userid','site','ts','duration'])
print('Time spent online per web site')
print(df.groupby('site').agg({'duration': 'sum'}))
print('Time spent online per web site, per year')
print(df.groupby([df.ts.dt.year, 'site']).agg({'duration': 'sum'}))
# calculating stats
stats = df.groupby([df.ts.dt.year, 'site'], sort=True)['userid'] \
.count() \
.reset_index() \
.rename(columns={'userid':'visits'}) \
stats = stats.set_index(stats.ts.astype(str) + ': ' + stats.site) \
.drop(['ts','site'], axis=1)
# plot part
fig = plt.figure(figsize=(16,9))
ax = fig.add_subplot(111)
title = 'site statistics (visitors)'
stats.plot(kind='barh', ax=ax, title=title, color=['grey'], legend=None)
[ax.annotate(str(visits), (stats.values.max()/2, i))
for i, visits in enumerate(stats['visits'].tolist())]
plt.show()
fig.savefig('stats.png',dpi=100,bbox='Tight')
输出:
^{pr2}$
PS请始终提供样本输入(5-10行)和预期输出(基于样本数据)