Python 数据帧 : Seperate rows based on custom condition?

标签 python python-3.x pandas dataframe numpy

我的数据框包含三列 namecontentday

df

        content          day           name
    0     first_day      01-01-2017      marcus
    1     present        10-01-2017      marcus
    2     first_day      01-02-2017      marcus
    3     first_day      01-03-2017      marcus
    4     absent         05-03-2017      marcus
    5     present        20-03-2017      marcus
    6     first_day      01-04-2017      bruno
    7     present        11-04-2017      bruno
    8     first_day      01-05-2017      bruno
    9     absent         02-05-2017      bruno
    10    first_day      01-06-2017      bruno
    11    absent         02-06-2017      bruno
    12    payment        09-06-2017      bruno

我试图找出 month wise 的用户,其行有 first_dayabsentpresent 连续.

示例输出:

        content          day           name         absent_after_present
    0     first_day      01-01-2017      marcus         False
    1     first_day      01-02-2017      marcus         False
    2     first_day      01-03-2017      marcus         True
    3     first_day      01-04-2017      bruno          False
    4     first_day      01-05-2017      bruno          False
    5     first_day      01-06-2017      bruno          True

例如:marcus first_day缺席present01-03-2017 连续05-03-201720-03-2017 同一个月。所以 marcus 状态应该是 True

最佳答案

也许您可以尝试提取每月的内容,然后按名称和月份分组,如下所示。

import pandas as pd

data = pd.DataFrame({'content' : ['first_day','present', 'first_day', 'first_day', 'absent', 
'present', 'first_day', 'present', 'first_day', 'absent', 'first_day', 'absent', 'present'],
'day' : ['2017-01-01', '2017-01-10', '2017-02-01', '2017-03-01', '2017-03-05', '2017-03-20',
'2017-04-01', '2017-04-11', '2017-05-01', '2017-05-02', '2017-06-01', '2017-06-02', '2017-06-09'],
'name' : ['marcus', 'marcus', 'marcus', 'marcus', 'marcus', 'marcus', 'bruno', 'bruno', 'bruno',
'bruno', 'bruno', 'bruno', 'bruno']})

data['day'] = pd.to_datetime(data['day'])

data['month'] = data.day.dt.month

data_new = pd.DataFrame(data.groupby(['name', 'month'])['content'].unique()).join(pd.DataFrame(data.groupby(['name', 'month'])['day'].unique()), on=['name', 'month'])

data_new['absent_after_present'] = data_new['content'].apply(lambda x : True if len(x) == 3 and len(set(x)) == 3 else False)
data_new['day'] = data_new['day'].apply(lambda x : x[0])
data_new['content'] = data_new['content'].apply(lambda x : x[0])

data_new = data_new.droplevel(1)



data_new


name    content        day  absent_after_present

bruno   first_day   2017-04-01  False
bruno   first_day   2017-05-01  False
bruno   first_day   2017-06-01  True
marcus  first_day   2017-01-01  False
marcus  first_day   2017-02-01  False
marcus  first_day   2017-03-01  True

关于Python 数据帧 : Seperate rows based on custom condition?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67018896/

相关文章:

Python Pandas : Convert Minutes to Datetime

python - 如何在分类类型上设置索引?

python - 测试与 2 元组序列中的第一项元组匹配的最 Pythonic 方法是什么?

python - Tensorflow 上的多 GPU 训练速度比单 GPU 慢

python - 两个进程之间的JoinableQueue,两个进程有时会永远阻塞

python - 如何使用 cherrypy 内置 ssl 模块(python 3)禁用 SSL3 和弱密码

python - 具有不确定性的两个数据点的线性拟合的置信区间

python - session 管理的 Itdangerous 存在安全问题吗?

Pandas 绘制多个数据帧,一个数据帧产生一条平坦线

python - 如何将日期格式(星期几、月、日、年)更改为 pandas 可读格式?