题解 | #牛客网连续练习题目3天及以上的用户#
牛客网连续练习题目3天及以上的用户
https://www.nowcoder.com/practice/4d77709a0ea2482282ee86852fe32f06
from datetime import timedelta import pandas as pd pd.set_option('display.width',300) pd.set_option('display.max_rows',None) pd.set_option('display.max_columns',None) file = pd.read_csv("nowcoder.csv", sep=',', dtype=object, parse_dates=True) file['date'] = pd.to_datetime(file.date).dt.date new = pd.merge(file, file, how='left', on='user_id') new['diff'] = new.date_y - new.date_x a = new.groupby(['user_id','date_x']).max() b = a[a['diff']>=timedelta(days=2)].groupby('user_id')['diff'].max() print(b)