使用Pandas选择每个PeriodIndex中最早的行

使用Pandas选择每个PeriodIndex中最早的行,pandas,Pandas,我有一个带有日期列的pandas数据框,我已将其转换为基于季度的PeriodIndex: a b c timestamp quarter 2018Q3 45 14 45 2018-09-04 14:15:24.168273-05:00 2018Q3* 45 14 45 2018-09-07 11:39:30.121899-05:00 2018Q4* 44 13 42 2018-12-11 10:36:19.915830-0

我有一个带有日期列的pandas数据框,我已将其转换为基于季度的PeriodIndex:

       a    b   c   timestamp
quarter             
2018Q3  45  14  45  2018-09-04 14:15:24.168273-05:00
2018Q3* 45  14  45  2018-09-07 11:39:30.121899-05:00
2018Q4* 44  13  42  2018-12-11 10:36:19.915830-06:00
2019Q1  44  12  41  2019-03-04 08:06:57.744857-06:00
2019Q1  44  12  41  2019-03-04 08:42:03.814000-06:00
2019Q1* 44  12  41  2019-03-04 08:55:57.174777-06:00
2019Q2  44  12  41  2019-04-03 14:47:19.942471-05:00
2019Q2  44  12  41  2019-04-05 02:54:08.998211-05:00
2019Q2  44  12  41  2019-05-05 02:52:38.556931-05:00
2019Q2* 44  12  41  2019-06-03 11:36:55.218757-05:00
我想为每个季度选择最近的行(上面用*表示的行)

  • 我看过重采样,但我不想用 聚合函数,因为我想丢弃来自另一个函数的数据 排。我正在考虑使用自定义应用函数进行重采样,但我 熊猫是新手,我在那里进展不大

  • 我一直在努力寻找可以跨行操作的选择逻辑 比较

  • 我可以通过循环和保留 每个季度都是最老的一排,但我对熊猫还不熟悉,所以我想 学习熊猫的方式

下面是一些生成示例数据帧的代码。(我不知道如何直接导出数据帧,因为PeriodIndex-to_dict/from_dict丢失了行):


如果我理解正确,您需要:
df.groupby(df.index)['timestamp'].max()。这就给了我想要的日期行。(谢谢!)。我只需要使用这些索引返回到主表中,以获得整行。然后您需要使用
transform
df[df['timestamp'].eq(df.groupby(df.index)['timestamp'].transform('max'))]
太棒了!这正是我需要的。如果你能把它作为一个答案,我会接受的。不管怎样,对于一个有真实答案的熊猫问题+1…如果我理解正确,你想要:
df.groupby(df.index)['timestamp'].max()。这就给了我想要的日期行。(谢谢!)。我只需要使用这些索引返回到主表中,以获得整行。然后您需要使用
transform
df[df['timestamp'].eq(df.groupby(df.index)['timestamp'].transform('max'))]
太棒了!这正是我需要的。如果你能把它作为一个答案,我会接受的。不管怎样,一个熊猫的问题加上一个真实的。。。
import datetime
import pandas as pd 
df_dict = {'customer_id': {5129: 123,
  2808: 123,
  2806: 123,
  5109: 123,
  5051: 123,
  2791: 123,
  5024: 123,
  3096: 123,
  4907: 123,
  6033: 123},
 'date': {5129: datetime.datetime(2019, 6, 3, 12, 17, 47, 903740, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=-300, name=None)),
  2808: datetime.datetime(2019, 3, 4, 8, 55, 57, 174777, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=-360, name=None)),
  2806: datetime.datetime(2019, 3, 4, 8, 55, 57, 174777, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=-360, name=None)),
  5109: datetime.datetime(2019, 6, 3, 12, 17, 47, 903740, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=-300, name=None)),
  5051: datetime.datetime(2019, 6, 3, 11, 43, 44, 144986, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=-300, name=None)),
  2791: datetime.datetime(2019, 3, 4, 8, 55, 57, 174777, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=-360, name=None)),
  5024: datetime.datetime(2019, 6, 3, 11, 43, 44, 144986, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=-300, name=None)),
  3096: datetime.datetime(2018, 9, 4, 14, 15, 24, 168273, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=-300, name=None)),
  4907: datetime.datetime(2019, 3, 4, 8, 6, 57, 744857, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=-360, name=None)),
  6033: datetime.datetime(2019, 6, 6, 9, 22, 47, 516471, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=-300, name=None))},
 'a': {5129: 0.0,
  2808: 140.0,
  2806: 138.0,
  5109: 0.0,
  5051: 0.0,
  2791: 0.0,
  5024: 0.0,
  3096: 0.0,
  4907: 0.0,
  6033: 0.0},
 'b': {5129: 0.0,
  2808: 0.0,
  2806: 0.0,
  5109: 16507.0,
  5051: 0.0,
  2791: 0.0,
  5024: 0.0,
  3096: 0.0,
  4907: 16498.0,
  6033: 16500.0},
 'c': {5129: 65.0,
  2808: 0.0,
  2806: 0.0,
  5109: 0.0,
  5051: 26748.0,
  2791: 26750.0,
  5024: 6.0,
  3096: 11.0,
  4907: 0.0,
  6033: 0.0}}
df = pd.DataFrame.from_dict(df_dict)
pivot = pd.pivot_table(df, index=['customer_id', 'date'], values=['a', 'b', 'c'], aggfunc='count')
for customer, data in pivot.groupby('customer_id'):
    df = data  # just grab the last one for SO example
df['timestamp'] = df.index.get_level_values('date')
df.index = pd.PeriodIndex(df.index.get_level_values('date'), freq='Q')
df