计算熊猫数据框中的相同日期

时间:2018-07-13 19:34:08

标签: python pandas date

我有一个带有日期列的数据框,我想创建一个新列来告诉我数据集包含多少个相同的日期。这是原始数据集的最小值示例:

df1:

var line = "14*x1 + 2*x2 + 3*x3 = 2";

var matches = Regex.Matches(line, @"(\d+)\*");

// [] { 14, 2 , 3 }
var numbers = matches.Select(x => double.Parse(x.Groups[1].Value)).ToArray();

我想创建此date_count,因此目标数据集为:

df1:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)  
    If ScanColor(Range("lockdown")) Then
        MsgBox "You have an invalid cell!"
    End If
End Sub

Private Function ScanColor(Cells As Range) As Boolean
    Dim cell As Range
    For Each cell In Cells
        If cell.displayformat.Interior.ColorIndex = 3 Then
            ScanColor = True
            Exit For
        End If
    Next
End Function

创建df1的实际代码:

date         
2017/01/03     
2017/01/03     
2017/01/04     
2017/01/04     
2017/01/04     
2017/01/05     

2 个答案:

答案 0 :(得分:5)

这是使用mapgroupby and size的另一种方法:

accept

答案 1 :(得分:3)

count transform

df['count'] = df.groupby('date')['date'].transform('count')

         date  count
0  2017/01/03      2
1  2017/01/03      2
2  2017/01/04      3
3  2017/01/04      3
4  2017/01/04      3
5  2017/01/05      1