Guest User

Untitled

a guest
Feb 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  1. """
  2. Path2Pro
  3. This code is used to organize data from daily tournaments
  4. """
  5. import numpy as np
  6. import pandas as pd
  7. import gspread_pandas as gp
  8. from oauth2client.service_account import ServiceAccountCredentials
  9. from gspread_pandas import Spread
  10. from collections import OrderedDict
  11.  
  12. tournament = Spread('league','Path2Pro League (Season 1)')
  13. tournament.sheets
  14.  
  15. ### pull tournament data into dataframes
  16. ### PLAYER NAMES
  17. tournament.open_sheet(5)
  18. tournament
  19. players = tournament.sheet_to_df(header_rows=1, index=True)
  20.  
  21. ### PLAYER HASHTAGS
  22. tournament.open_sheet(6)
  23. tournament
  24. hashtags = tournament.sheet_to_df(header_rows=1, index=True)
  25.  
  26. ### PLAYER TROPHIES
  27. tournament.open_sheet(7)
  28. tournament
  29. trophies = tournament.sheet_to_df(header_rows=1, index=True)
  30.  
  31. ### PLAYER CLANS
  32. tournament.open_sheet(8)
  33. tournament
  34. clans = tournament.sheet_to_df(header_rows=1, index=True)
  35.  
  36. ### PLAYER WINLOSS
  37. tournament.open_sheet(9)
  38. tournament
  39. winloss = tournament.sheet_to_df(header_rows=1, index=True)
  40.  
  41. ### PLAYER GAMES
  42. tournament.open_sheet(10)
  43. tournament
  44. games = tournament.sheet_to_df(header_rows=1, index=True)
  45.  
  46. ### PLAYER PARTICIPATION
  47. tournament.open_sheet(11)
  48. tournament
  49. participation = tournament.sheet_to_df(header_rows=1, index=True)
  50.  
  51. ### PLAYER PLACEMENT
  52. tournament.open_sheet(12)
  53. tournament
  54. placement = tournament.sheet_to_df(header_rows=1, index=True)
  55.  
  56. ### PLAYER WINS
  57. tournament.open_sheet(13)
  58. tournament
  59. wins = tournament.sheet_to_df(header_rows=1, index=True)
  60.  
  61. ### PLAYER LOSSES
  62. tournament.open_sheet(14)
  63. tournament
  64. losses = tournament.sheet_to_df(header_rows=1, index=True)
  65.  
  66.  
  67. ### reshape to get dates into rows
  68. hashtags_reshaped = pd.melt(hashtags, id_vars = ['Rank'],
  69. value_vars = hashtags.columns,
  70. var_name = 'Date',
  71. value_name = 'Code').drop('Rank', axis = 1)
  72.  
  73. players_reshaped = pd.melt(players, id_vars = ['Rank'],
  74. value_vars = hashtags.columns,
  75. var_name = 'Date',
  76. value_name = 'Name').drop('Rank', axis = 1)
  77.  
  78. clans_reshaped = pd.melt(clans, id_vars = ['Rank'],
  79. value_vars = hashtags.columns,
  80. var_name = 'Date',
  81. value_name = 'Clan').drop('Rank', axis = 1)
  82.  
  83. trophies_reshaped = pd.melt(trophies, id_vars = ['Rank'],
  84. value_vars = hashtags.columns,
  85. var_name = 'Date',
  86. value_name = 'Score').drop('Rank', axis = 1)
  87.  
  88. winloss_reshaped = pd.melt(winloss, id_vars = ['Rank'],
  89. value_vars = hashtags.columns,
  90. var_name = 'Date',
  91. value_name = 'Winloss').drop('Rank', axis = 1)
  92.  
  93. games_reshaped = pd.melt(games, id_vars = ['Rank'],
  94. value_vars = hashtags.columns,
  95. var_name = 'Date',
  96. value_name = 'Games').drop('Rank', axis = 1)
  97.  
  98. participation_reshaped = pd.melt(participation, id_vars = ['Rank'],
  99. value_vars = hashtags.columns,
  100. var_name = 'Date',
  101. value_name = 'Participation').drop('Rank', axis = 1)
  102.  
  103. placement_reshaped = pd.melt(placement, id_vars = ['Rank'],
  104. value_vars = hashtags.columns,
  105. var_name = 'Date',
  106. value_name = 'Placement').drop('Rank', axis = 1)
  107.  
  108. wins_reshaped = pd.melt(wins, id_vars = ['Rank'],
  109. value_vars = hashtags.columns,
  110. var_name = 'Date',
  111. value_name = 'Wins').drop('Rank', axis = 1)
  112.  
  113. losses_reshaped = pd.melt(losses, id_vars = ['Rank'],
  114. value_vars = hashtags.columns,
  115. var_name = 'Date',
  116. value_name = 'Losses').drop('Rank', axis = 1)
  117.  
  118. ### merge together
  119. merged_df = pd.DataFrame([hashtags_reshaped['Date'], pd.to_numeric(winloss_reshaped['Winloss']), pd.to_numeric(wins_reshaped['Wins']), pd.to_numeric(losses_reshaped['Losses']), pd.to_numeric(participation_reshaped['Participation']), pd.to_numeric(games_reshaped['Games']), hashtags_reshaped['Code'], players_reshaped['Name'], pd.to_numeric(placement_reshaped['Placement']), pd.to_numeric(trophies_reshaped['Score'])]).T
  120. print(merged_df)
  121.  
  122. ### group by code, name, and date; sum the scores together if multiple exist for a given code-name-date grouping
  123. grouped_df = merged_df.groupby(['Code', 'Name', 'Date']).sum().sort_values('Score', ascending = False)
  124. print(grouped_df)
  125.  
  126. ### sum together
  127. summed_df = merged_df.drop('Date', axis = 1) \
  128. .groupby(['Code', 'Name']).sum() \
  129. .sort_values('Score', ascending = False).reset_index()
  130. summed_df['li'] = list(zip(summed_df.Name, summed_df.Score))
  131. print(summed_df)
  132.  
  133. ### REGISTERED USERS
  134. tournament.open_sheet(4)
  135. tournament
  136. registered = tournament.sheet_to_df(header_rows=1, index=False)
  137. registered_list = registered['CR Tag #'].tolist()
  138. print(registered_list)
  139.  
  140. ### Filter total participant by registered users
  141. participants_score = summed_df[summed_df['Code'].isin(registered_list)]
  142.  
  143. tournament.df_to_sheet(participants_score, sheet='Output')
  144. print(tournament)
  145.  
  146. participants_score.info()
Add Comment
Please, Sign In to add comment