0

I'm going beyond this question to get further information about a datetime dataframe. Working with a DataFrame like this:

User_ID    Datetime
01    2014-01-01 08:00:00
01    2014-02-02 09:00:00
02    2014-01-02 10:00:00
02    2014-01-03 11:00:00
03    2014-01-04 12:00:00
04    2014-01-04 13:00:00
05    2014-01-02 14:00:00
01    2014-04-01 08:00:00
01    2014-03-02 09:00:00
01    2014-05-01 08:00:00
01    2014-06-02 09:00:00
01    2014-07-01 08:00:00
01    2014-08-02 09:00:00
01    2014-09-01 08:00:00
01    2014-10-02 09:00:00
01    2014-11-01 08:00:00
01    2014-12-02 09:00:00

where Users are associated to a particular datetime event. Users can have more occurrences during the year, for instance 12 per month, 100 per day etc, but I would like to filter only the users with AT LEAST one occurrence / every single month of the year. In the above example the User 01.

2 Answers 2

2

Here is an alternative way to do it using pandas magic:

df['Datetime'] = pd.to_datetime(df.Datetime)  # You can skip this step if you already have it as Datatime object
df1 = df.groupby(['User_ID', df.Datetime.dt.year]).apply(lambda x: x.Datetime.dt.month.nunique())
ids = df1[df1 >= 12].index.get_level_values('User_ID')
df[df.User_ID.isin(ids)]

Which yields:

    User_ID            Datetime
0         1 2014-01-01 08:00:00
1         1 2014-02-02 09:00:00
7         1 2014-04-01 08:00:00
8         1 2014-03-02 09:00:00
9         1 2014-05-01 08:00:00
10        1 2014-06-02 09:00:00
11        1 2014-07-01 08:00:00
12        1 2014-08-02 09:00:00
13        1 2014-09-01 08:00:00
14        1 2014-10-02 09:00:00
15        1 2014-11-01 08:00:00
16        1 2014-12-02 09:00:00
Sign up to request clarification or add additional context in comments.

Comments

1

This is my solution supposing you work on a single year.

df is your DataFrame

for user in df.User_ID.unique():

   months = []

   for d in df.Datetime[df.User_ID == user]:
      months.append(d.month)
   if( len(list(set(months))) == 12):
      print('Im user ' + str(user))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.