0

I have this question augment the data so that each county contains the relative vote count for each party in each election.

(e.g. For each county, if the Republicans got 2300 votes and the Democrats got 1900 votes, then the relative vote shares should be 54% and 46%.) enter image description here

  • please provide sample input and reproducible sample output: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples (Probably very easy for someto help if you can do that) – David Erickson Jul 17 '20 at 00:04

1 Answers1

0

Judging based of the data you provided in the picture:

df_perc = df[['Democrats_12(Votes)','Republican_12(Votes)',
          'Democrats_16(Votes)','Republican_16(Votes)']].div(df.sum(axis=1),
           axis = 0)

If you wanted to add the Democrat and Republican Columns together:

df_perc['Democrat'] = df_perc['Democrats_12(Votes)'] + df_perc['Democrats_16(Votes)']
df_perc['Republican'] = df_perc['Republican_12(Votes)'] + df_perc['Republican_16(Votes)']
nav610
  • 781
  • 3
  • 8