熊猫列按日期计数

时间:2019-10-16 20:06:31

标签: pandas-groupby

我有一个具有日期时间索引的数据框。我想添加一列来保存当天的行数。

dff = pd.DataFrame(['red','red','blue'],
    columns = ['colors'],
    index = [pd.Timestamp('2019-09-19 14:03:20'),pd.Timestamp('2019-09-19 17:03:20'),pd.Timestamp('2019-09-20 14:03:20')])

                    colors
2019-09-19 14:03:20 red
2019-09-19 17:03:20 red
2019-09-20 14:03:20 blue

因此,发生在2019-09-19的行的“计数”列应为2,最后一行的计数列应为1。

1 个答案:

答案 0 :(得分:1)

这会临时创建一个仅包含日期的列,然后对所述列进行计数并将其放入真实数据帧中称为counts的新列中。

XAML

这里完整地粘贴到IDE中并进行测试:

<Frame Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" BorderColor="Black" Margin="0" Padding="0">
     <controls:CustomEditor HeightRequest="80" IsPassword="True">
         <controls:CustomEditor.PlainText>
             <OnPlatform x:TypeArguments="BindingBase">
                 <On Platform="iOS" Value="{Binding KeyString}"/>
             </OnPlatform>
         </controls:CustomEditor.PlainText>

         <controls:CustomEditor.Text>
             <OnPlatform x:TypeArguments="BindingBase">
                 <On Platform="Android" Value="{Binding KeyString}"/>
             </OnPlatform>
         </controls:CustomEditor.Text>

         <controls:CustomEditor.Effects>
             <controls:PasswordEffect>
             </controls:PasswordEffect>
         </controls:CustomEditor.Effects>
     </controls:CustomEditor>
</Frame>

结果:

using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;

namespace MyApp.CustomControls
{
    public class CustomEditor : Editor
    {
        public static readonly BindableProperty IsPasswordProperty =
         BindableProperty.Create(nameof(IsPassword), typeof(bool), typeof(CustomEditor), false);

        public static readonly BindableProperty PlainTextProperty =
            BindableProperty.Create(nameof(PlainText),
                typeof(string),
                typeof(CustomEditor),
                String.Empty,
                defaultBindingMode:BindingMode.TwoWay,
                propertyChanged:OnPlainTextChanged);

        public bool IsPassword
        {
            get { return (bool)GetValue(IsPasswordProperty); }
            set { SetValue(IsPasswordProperty, value); }
        }

        public string PlainText {
            get { return (string)GetValue(PlainTextProperty); }
            set { SetValue(PlainTextProperty, value); }
        }

        private static void OnPlainTextChanged(BindableObject bindable, object oldValue, object newValue)
        {
            var control = (CustomEditor)bindable;
            if (newValue != null)
            {
                control.PlainText = newValue.ToString();
            }
        }
    }
}