Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. import pandas as pd
  2.  
  3. data = pd.read_csv("/datasets/visits.csv", sep="\t")
  4. data['local_time'] = (
  5. pd.to_datetime(data['date_time'], yearfirst=True)
  6. + pd.Timedelta(hours=3)
  7. )
  8. data['date_hour'] = data['local_time'].dt.round('1H')
  9. data['too_fast'] = data['time_spent'] < 60
  10. data['too_slow'] = data['time_spent'] > 1000
  11.  
  12. too_fast_stat = data.pivot_table(index='id', values='too_fast')
  13. good_ids = too_fast_stat.query('too_fast < 0.5')
  14. good_data = data.query('id in @good_ids.index')
  15. good_data = good_data.query('60 <= time_spent <= 1000')
  16.  
  17. station_stat = data.pivot_table(index="id", values="time_spent", aggfunc="median")
  18. good_station_stat = good_data.pivot_table(index="id", values="time_spent", aggfunc="median")
  19.  
  20. stat = data.pivot_table(index='name', values='time_spent')
  21. good_stat = good_data.pivot_table(index='name', values='time_spent', aggfunc='median')
  22. stat['good_time_spent'] = good_stat['time_spent']
  23.  
  24. id_name = good_data.pivot_table(index='id', values='name', aggfunc=['first', 'count'])
  25. id_name.columns = ['name', 'count']
  26. station_stat_full = id_name.join(good_station_stat)
  27.  
  28. good_stat2 = (
  29. station_stat_full
  30. .query('count > 30')
  31. .pivot_table(index='name', values='time_spent', aggfunc=['median', 'count'])
  32. )
  33. good_stat2.columns = ['median_time', 'stations']
  34. final_stat = stat.join(good_stat2)
  35.  
  36. big_nets_stat = final_stat.query('stations > 10')
  37.  
  38. station_stat_full['group_name'] = (
  39. station_stat_full['name']
  40. .where(station_stat_full['name'].isin(big_nets_stat.index), 'Другие')
  41. )
  42.  
  43. stat_grouped = (
  44. station_stat_full
  45. .query('count > 30')
  46. .pivot_table(index='group_name', values='time_spent', aggfunc=['median', 'count'])
  47. )
  48. stat_grouped.columns = ['time_spent', 'count']
  49.  
  50. good_data['group_name'] = (
  51. good_data['name']
  52. .where(good_data['name'].isin(big_nets_stat.index), 'Другие')
  53. )
  54. for name, group_data in good_data.groupby('group_name'):
  55. name, len(group_data.hist('time_spent', bins = 50))
  56. good_data.plot(kind = 'hist', y = 'time_spent', title = name, bins = 50)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement