Guest User

nyt_ts

a guest
Nov 11th, 2020
562
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3.  
  4. # In[18]:
  5.  
  6.  
  7. import pandas as pd
  8. import numpy as np
  9. from IPython.core.interactiveshell import InteractiveShell
  10. InteractiveShell.ast_node_interactivity = "all"
  11.  
  12.  
  13. # In[19]:
  14.  
  15.  
  16. def index_prev_next(ix):    
  17.     ix_prev = [i-1 for i in ix]
  18.     ix_next = [i+1 for i in ix]
  19.     ix_present = ix_prev + list(ix) + list(ix_next)
  20.     ix_present.sort()
  21.     return ix_present
  22.  
  23.  
  24. # ## Data Prep
  25.  
  26. # In[20]:
  27.  
  28.  
  29. path = "../data_files/Election/"
  30. csv_file = "nyt_ts.csv"
  31. csv_path = path + csv_file
  32.  
  33. df_nyt_ts = pd.read_csv(csv_path)
  34.  
  35.  
  36. # In[21]:
  37.  
  38.  
  39. df_nyt_ts.timestamp = pd.to_datetime(df_nyt_ts.timestamp)
  40.  
  41.  
  42. # In[22]:
  43.  
  44.  
  45. df_nyt_ts = df_nyt_ts.query("votes > 0")
  46.  
  47.  
  48. # In[23]:
  49.  
  50.  
  51. df_nyt_ts['votes_rep'] = df_nyt_ts.votes * df_nyt_ts.vote_share_rep
  52. df_nyt_ts['votes_dem'] = df_nyt_ts.votes * df_nyt_ts.vote_share_dem
  53.  
  54.  
  55. # In[24]:
  56.  
  57.  
  58. df_nyt_ts.sort_values(['state','timestamp'], inplace=True)
  59. df_nyt_ts.reset_index(inplace=True, drop=True)
  60.  
  61.  
  62. # In[25]:
  63.  
  64.  
  65. vote_cols = ['votes','votes_rep','votes_dem']
  66. vote_diff_cols = [c+"_diff" for c in vote_cols]
  67.  
  68. df_nyt_ts[vote_diff_cols] = df_nyt_ts.groupby(['state'])[vote_cols].diff()
  69.  
  70.  
  71. # In[26]:
  72.  
  73.  
  74. df_nyt_ts['compare_rep'] = df_nyt_ts.eval("votes_rep_diff / votes_diff")
  75. df_nyt_ts['compare_dem'] = df_nyt_ts.eval("votes_dem_diff / votes_diff")
  76.  
  77.  
  78. # ## Biased Vote Dump
  79.  
  80. # In[27]:
  81.  
  82.  
  83. cols = ['state','timestamp','vote_share_rep','vote_share_dem','votes',
  84.         'votes_rep','votes_dem','votes_diff','votes_rep_diff','votes_dem_diff']
  85.  
  86.  
  87. # In[28]:
  88.  
  89.  
  90. threshold_dump = 10**5
  91. threshold_compare = 0.1
  92.  
  93.  
  94. # ### Unfavotable for Trump
  95.  
  96. # In[30]:
  97.  
  98.  
  99. df = df_nyt_ts.query("(votes_diff > @threshold_dump) & (compare_rep < @threshold_compare)")
  100. ix_present = index_prev_next(df.index)
  101. df_xlsx = df_nyt_ts.loc[ix_present][cols + ['compare_rep']]
  102. df_xlsx
  103. df_xlsx.to_excel("vote_dump_against_trump.xlsx")
  104.  
  105.  
  106. # ### Unfavotable for Biden
  107.  
  108. # In[31]:
  109.  
  110.  
  111. df = df_nyt_ts.query("(votes_diff > @threshold_dump) & (compare_dem < @threshold_compare)")
  112. ix_present = index_prev_next(df.index)
  113. df_xlsx = df_nyt_ts.loc[ix_present][cols + ['compare_rep']]
  114. df_xlsx
  115. df_xlsx.to_excel("vote_dump_against_biden.xlsx")
  116.  
  117.  
  118. # ## Vote Switch
  119.  
  120. # In[32]:
  121.  
  122.  
  123. flt = (df_nyt_ts.votes_rep_diff == df_nyt_ts.votes_dem_diff*-1)
  124. flt2 = (df_nyt_ts.votes_rep_diff != 0)
  125. df = df_nyt_ts[flt & flt2].copy()
  126. ix_present = index_prev_next(df.index)
  127. df_xlsx = df_nyt_ts.reindex(ix_present)[cols]
  128. df_xlsx
  129. df_xlsx.to_excel("vote_switch.xlsx")
  130.  
  131.  
  132. # In[33]:
  133.  
  134.  
  135. threshold_switch = 20
  136. flt = np.abs(df_nyt_ts.votes_rep_diff - df_nyt_ts.votes_dem_diff*-1) < threshold_switch
  137. flt2 = (df_nyt_ts.votes_rep_diff != 0)
  138. flt3 = np.sign(df_nyt_ts.votes_rep_diff * df_nyt_ts.votes_dem_diff) < 0
  139. df = df_nyt_ts[flt & flt2 & flt3].copy()
  140. ix_present = index_prev_next(df.index)
  141. df_xlsx = df_nyt_ts.reindex(ix_present)[cols]
  142. df_xlsx
  143. df_xlsx.to_excel("vote_switch_within_20.xlsx")
  144.  
  145.  
  146. # ## Votes Decrease
  147.  
  148. # In[36]:
  149.  
  150.  
  151. thresh_vote_dec = -10**5
  152. ix_diff_neg = df_nyt_ts.query("(votes_diff < @thresh_vote_dec)").index
  153. ix_present = index_prev_next(ix_diff_neg)
  154. df_xlsx = df_nyt_ts.loc[ix_present,cols]
  155. df_xlsx
  156. df_xlsx.to_excel("vote_decrease.xlsx")
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×