如何根据特定列表计算频率?(How do I count the frequency against a specific list?)

编程入门 行业动态 更新时间:2024-10-27 20:39:30
如何根据特定列表计算频率?(How do I count the frequency against a specific list?)

我有一个看起来像这样的DataFrame 。

date name 0 2015-06-13 00:21:25 a 1 2015-06-13 01:00:25 b 2 2015-06-13 02:54:48 c 3 2015-06-15 14:38:15 a 4 2015-06-15 15:29:28 b

我想计算特定日期范围内日期的出现次数,包括那些未出现在列中的日期(并忽略name列中的任何内容)。 例如,我的日期范围可能如下所示:

periods = pd.date_range('2015-06-13', '2015-06-16', freq = 'd')

然后,我想要一个看起来像这样的输出:

date count 2015-06-13 3 2015-06-14 0 2015-06-15 2 2015-06-16 0

我找不到任何让我保留0行的函数。

I have a DataFrame that looks like this.

date name 0 2015-06-13 00:21:25 a 1 2015-06-13 01:00:25 b 2 2015-06-13 02:54:48 c 3 2015-06-15 14:38:15 a 4 2015-06-15 15:29:28 b

I want to count the occurrences of dates against a specific date range, including ones that do not appear in the column (and ignores whatever that is in the name column). For example, I might have a date range that looks like this:

periods = pd.date_range('2015-06-13', '2015-06-16', freq = 'd')

Then, I want an output that looks something like:

date count 2015-06-13 3 2015-06-14 0 2015-06-15 2 2015-06-16 0

I haven't been able to find any function that let me keep the 0 rows.

最满意答案

我认为您可以首先使用列date为value_counts ,然后使用fillna的periods重新reindex为0 。 最后通过astype和reset_index将float转换为int :

df = df['date'].dt.date.value_counts() print df 2015-06-13 3 2015-06-15 2 Name: date, dtype: int64 periods = pd.date_range('2015-06-13', '2015-06-16', freq = 'd') df = df.reindex(periods).fillna(0).astype(int).reset_index() df.columns = ['date','count'] print df date count 0 2015-06-13 3 1 2015-06-14 0 2 2015-06-15 2 3 2015-06-16 0

I think you can first use date from column date for value_counts and then reindex by periods with fillna by 0. Last convert float to int by astype and reset_index:

df = df['date'].dt.date.value_counts() print df 2015-06-13 3 2015-06-15 2 Name: date, dtype: int64 periods = pd.date_range('2015-06-13', '2015-06-16', freq = 'd') df = df.reindex(periods).fillna(0).astype(int).reset_index() df.columns = ['date','count'] print df date count 0 2015-06-13 3 1 2015-06-14 0 2 2015-06-15 2 3 2015-06-16 0

更多推荐

本文发布于:2023-08-06 05:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1445262.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:频率   列表   count   list   specific

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!