gpt4 book ai didi

python - 给定日期时间的 DataFrame,绘制每年的用户访问情况

转载 作者:行者123 更新时间:2023-12-01 09:17:25 27 4
gpt4 key购买 nike

根据下面的数据框,按年份绘制用户访问情况。

最好是折线图,其中 x 轴是年份,y 轴是当年的访问次数。

数据框:

In:
print df
Out:
0 2016-10-01
1 2015-11-05
2 2017-12-07
3 2016-08-09
4 2015-11-22
5 2016-12-13
6 2017-03-25
7 2016-09-11
8 2017-04-12
9 2016-08-29
10 2015-11-04
Name: date, dtype: datetime64[ns]

导入模块:matplotlib、pandas 和 seaborn:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

最佳答案

用途:

df['date'].dt.year.value_counts().sort_index().plot.bar()

详细信息:

print (df['date'].dt.year.value_counts().sort_index())
2015 3
2016 5
2017 3
Name: date, dtype: int64

说明:

  1. 首先通过 dt.year 将值转换为年份
  2. value_counts 计数与 sort_index因为 value_counts 中默认按最高值排序
  3. 最后一个情节 Series.plot.bar

graph

关于python - 给定日期时间的 DataFrame,绘制每年的用户访问情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51117661/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com