在Pandas中的groupby之后,日期时间列发生了更改

时间:2018-03-23 13:59:05

标签: python pandas

我有以下数据框:

while($regx=mysqli_fetch_array($resx)) {
    ?>
    ...
    <a href="#price-break-down-<?= $regx['id'] ?>" class="price-breakdown">
        <i class="fa fa-caret-right"></i>Lista de Preços
    </a>
    <div id="price-break-down-<?= $regx['id'] ?>" class="price-breakdown-popup mfp-hide">
        ...
    </div>
    ...
    <?php
}

对数据框进行排序和分组(获取第一个元素)后,> df.head(7) id user_id date_created_status date_created_user 24 10 2015-02-25 17:01:21-03:00 2015-02-25 17:00:27-03:00 26 1 2015-02-26 00:18:10-03:00 2015-02-23 16:37:58-03:00 29 9 2015-02-28 07:23:53-03:00 2015-02-25 16:12:11-03:00 30 7 2015-03-03 03:22:45-03:00 2015-02-24 01:24:08-03:00 31 7 2015-03-03 03:24:53-03:00 2015-02-24 01:24:08-03:00 38 13 2015-03-04 19:11:16-03:00 2015-03-04 19:09:27-03:00 39 14 2015-03-04 19:19:16-03:00 2015-03-04 19:17:47-03:00 date_created_status列会更改其日期格式。

date_created_user

如果我尝试在任何这些日期时间列中调用> df.sort_values('date_created_status', inplace=True) > df = df.groupby('user_id', as_index=False).first() id user_id date_created_status date_created_user 1 26 2015-02-26T03:18:10.000000000 2015-02-23T19:37:58.000000000 2 46352 2016-01-22 15:50:40.516000-02:00 2015-02-23 16:37:58-03:00 4 62 2015-03-10 17:14:27-03:00 2015-02-23 16:37:58-03:00 7 30 2015-03-03 03:22:45-03:00 2015-02-24 01:24:08-03:00 8 3274 2015-06-16 18:37:29.056000-03:00 2015-02-24 15:30:02-03:00 9 29 2015-02-28 07:23:53-03:00 2015-02-25 16:12:11-03:00 10 24 2015-02-25 17:01:21-03:00 2015-02-25 17:00:27-03:00 12 1223 2015-05-05 09:39:26.530000-03:00 2015-02-27 14:43:10-03:00 函数,我会收到错误:

strftime

如何在没有&#34;打破&#34;的情况下对这些行进行排序和分组。日期时间?

1 个答案:

答案 0 :(得分:1)

您可以在此使用head

df.sort_values('date_created_status', inplace=True)
df = df.groupby('user_id', as_index=False).head(1)

drop_duplicated

df.sort_values('date_created_status', inplace=True)
df=df.drop_duplicates('user_id',keep='first')