题解 | #牛客网连续练习题目3天及以上的用户#
牛客网连续练习题目3天及以上的用户
https://www.nowcoder.com/practice/4d77709a0ea2482282ee86852fe32f06
from datetime import timedelta
import pandas as pd
import numpy as np
# 1.读取数据
df = pd.read_csv('nowcoder.csv', sep=',', encoding='utf8')
# 2.保留2021年12月的用户
df['date'] = pd.to_datetime(df['date'])
df['date'] = df['date'].dt.strftime('%Y-%m-%d')
df['date'] = pd.to_datetime(df['date'])
df = df.loc[(df['date'].dt.year == 2021) & (df['date'].dt.month == 12)]
# 3.求连续练习三天以上的用户
# 3.1 排序
df.sort_values(by=['user_id', 'date'], ascending=True, inplace=True)
# 3.2 去重
df.drop_duplicates(subset=['user_id', 'date'], inplace=True)
# 3.3 分组求连续三天
def count_max_day(x):
max_day, count_day =1, 1 # 最大连续天数,连续天数
# 循环遍历每一天
for idx, val in x.iterrows():
now = val['date'] + timedelta(days=1)
# 最大能有多少天
for i in range(len(val)):
if x['date'].isin([now]).any():
count_day += 1
now = now + timedelta(days=1)
else:
break
# 求最大连续天数
max_day = max(max_day, count_day)
count_day = 1
return max_day
data = df.groupby(by=['user_id']).apply(count_max_day)
print(data[data >= 3])
