从零开始计数(Start counting at zero by group)
考虑以下数据框:
>>> import pandas as pd >>> df = pd.DataFrame({'group': list('aaabbabc')}) >>> df group 0 a 1 a 2 a 3 b 4 b 5 a 6 b 7 c
我想统计每个组发生的累计次数。 我想要的输出如下:
>>> df group n 0 a 0 1 a 1 2 a 2 3 b 0 4 b 1 5 a 3 6 b 2 7 c 0
我最初的做法是做这样的事情:
df['n'] = df.groupby('group').apply(lambda x: list(range(x.shape[0])))
基本上为每个组分配一个长度为n的数组,索引为零。 但这已证明难以转位和加入。
Consider the following dataframe:
>>> import pandas as pd >>> df = pd.DataFrame({'group': list('aaabbabc')}) >>> df group 0 a 1 a 2 a 3 b 4 b 5 a 6 b 7 c
I want to count the cumulative number of times each group has occurred. My desired output looks like this:
>>> df group n 0 a 0 1 a 1 2 a 2 3 b 0 4 b 1 5 a 3 6 b 2 7 c 0
My initial approach was to do something like this:
df['n'] = df.groupby('group').apply(lambda x: list(range(x.shape[0])))
Basically assigning a length n array, zero-indexed, to each group. But that has proven difficult to transpose and join.
原文:https://stackoverflow.com/questions/50175299
更新时间:2022-09-04 15:09
最满意答案
你可以做
SELECT LAST_DAY
,例如10月31日以下的退货。 2010SELECT LAST_DAY('2010-10-10');
I found a solution. but this query is very slow on large tables. so I am still looking for a better solution
select DISTINCT(LAST_DAY(date)) from table;
相关问答
更多-
有什么数据库专业书籍介绍?[2022-05-19]
mysql -
根据Babiker的回答 ,这是文档所说的 : 由于MySQL允许存储不完整的日期(例如'2014-00-00'),因此月份和日期说明符的范围从零开始。 Following from Babiker's answer, this is what the documentation says: Ranges for the month and day specifiers begin with zero due to the fact that MySQL allows the storing of inco ...
-
MySQL SELECT最近几天?(MySQL SELECT last few days?)[2022-11-23]
三天前使用日期: WHERE t.date >= DATE_ADD(CURDATE(), INTERVAL -3 DAY); 检查DATE_ADD文档。 或者你可以使用: WHERE t.date >= ( CURDATE() - INTERVAL 3 DAY ) Use for a date three days ago: WHERE t.date >= DATE_ADD(CURDATE(), INTERVAL -3 DAY); Check the DATE_ADD documentation. O ... -
最简单的选择是拥有一个日历表 ,其中包含月份的最后一天标记,因此您的查询将只是: SELECT * FROM dbo.Calendar WHERE Date >= @StartDate AND Date <= @EndDate AND EndOfMonth = 1; 假设您没有日历表,您可以动态生成日期列表:' DECLARE @s_date DATE = '20130101', @e_date DATE = '20130601'; SELECT Date ...
-
使用日历回复所有月份(Echo all months with days calendar)[2023-07-05]
尝试这个: function getDates($year) { $dates = array(); for($i = 1; $i <= 366; $i++){ $month = date('m', mktime(0,0,0,1,$i,$year)); $wk = date('W', mktime(0,0,0,1,$i,$year)); $wkDay = date('D', mktime(0,0,0,1,$i,$year)); ... -
你可以做SELECT LAST_DAY ,例如10月31日以下的退货。 2010 SELECT LAST_DAY('2010-10-10'); I found a solution. but this query is very slow on large tables. so I am still looking for a better solution select DISTINCT(LAST_DAY(date)) from table;
-
您可以尝试将当前月份的条件附加到当前查询,您可以将-1 , -2 , -3等等添加到AND MONTH...条件。 AND YEAR(`date_column`) = YEAR(NOW()) AND MONTH(`date_column`) = MONTH(NOW()) 其中date_column是您正在使用的列的名称。 也许你正在尝试GROUP BY MONTH( date_column )`它有点难以辨别。 You might try appending to your current query t ...
-
检查这两个功能: 对于月份: 期间差异天数: 日期差异 Check these 2 functions: For Months : Period Diff For days : Date Diff
-
保留一份有开始日期和结束日期的房间预订清单。 不要尝试为可用性插槽建模或将您的预订数据库视为电子表格。 这只会让你变得毫无意义。 日期范围易于使用。 最重要的是要知道如何在查询中检测重叠的日期范围。 这是测试房间是否已预留或是否免费的基础。 假设您有一个预订表,并且您希望找到与给定日期范围重叠的预订:@FromDate和@ToDate。 用于查找重叠预留的WHERE子句如下所示: WHERE RESERVATION.start_date < @ToDate AND RESERVATION.end_da ...
-
使用您对年/月长度的假设; function dToYMD(i) { var y, m ,d; y = (i / 365) | 0; i = i - y * 365; m = (i / 30) | 0; i = i - m * 30; d = i | 0; return [y, m, d]; } dToYMD(762); // [2 /* years */, 1 /* month */, 2 /* days */] 我以为内置了一 ...