我有这个问题 扩充数据,以便每个县包含每次选举中每个政党的相对投票数。
(例如,对于每个县,如果共和党获得2300票,民主党获得1900票,则相对投票份额应为54%和46%。) enter image description here
答案 0 :(得分:0)
根据您在图片中提供的数据进行判断:
df_perc = df[['Democrats_12(Votes)','Republican_12(Votes)',
'Democrats_16(Votes)','Republican_16(Votes)']].div(df.sum(axis=1),
axis = 0)
如果您想将“民主党”和“共和党专栏”加在一起:
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)']