【发布时间】:2020-04-03 15:16:20
【问题描述】:
我有一个投票数据框,我想创建一个偏好。 例如这里是每个城市 Comm, Comm2 中每一方 P1, P2, P3 的票数...
Comm Votes P1 P2 P3
0 comm1 1315.0 2.0 424.0 572.0
1 comm2 4682.0 117.0 2053.0 1584.0
2 comm3 2397.0 2.0 40.0 192.0
3 comm4 931.0 2.0 12.0 345.0
4 comm5 842.0 47.0 209.0 76.0
... ... ... ... ... ...
1524 comm1525 10477.0 13.0 673.0 333.0
1525 comm1526 2674.0 1.0 55.0 194.0
1526 comm1527 1691.0 331.0 29.0 78.0
这些选举结果足以首次通过投票系统,我想测试the alternative election model。因此,我需要了解每个政党的偏好。
由于我不知道偏好,我想用随机数制作它们。我想选民是诚实的。例如,对于城镇“comm”中的“P1”政党,我们知道有 2 人投票支持它,并且有 1315 名选民。我需要创建偏好,看看人们是否会将其作为他们的第一、第二或第三选择。也就是说,对于每一方:
Comm Votes P1_1 P1_2 P1_3 P2_1 P2_2 P2_3 P3_1 P3_2 P3_3
0 comm1 1315.0 2.0 1011.0 303.0 424.0 881.0 10.0 570.0 1.0 1.0
... ... ... ... ... ...
1526 comm1527 1691.0 331.0 1300.0 60.0 299.0 22.0 10.0 ...
所以我必须这样做:
# for each column in parties I create (parties -1) other columns
# I rename them all Party_i. The former 1 becomes Party_1.
# In the other columns I put a random number.
# For a given line, the sum of all Party_i for i in [1, parties] mus t be equal to Votes
到目前为止我已经试过了:
parties = [item for item in df.columns if item not in ['Comm','Votes']]
for index, row in df_test.iterrows():
# In the other columns I put a random number.
for party in parties:
# for each column in parties I create (parties -1) other columns
for i in range(0,len(parties) -1):
print(random.randrange(0, row['Votes']))
# I rename them all Party_i. The former 1 becomes Party_1.
row["{party}_{preference}".format(party = party,preference = i)] = random.randrange(0, row['Votes']) if (row[party] < row['Votes']) else 0 # false because the sum of the votes isn't = to df['Votes']
结果是:
Comm Votes ... P1_1 P1_2 P1_3 P2_1 P2_2 P2_3 P3_1 P3_2 P3_3
0 comm1 1315.0 ... 1003 460 1588 1284 1482 1613 1429 345
1 comm2 1691.0 ... 1003 460 1588 1284 1482 1613 ...
...
但是:
- 每行的数字都相同
-
Pi_1行中的值不等于Pi行中的值(Pi是给定的一方)。 - [0,party] 中所有 j 的
Pi_j总和不等于Votes列中的数字
更新
我用他自己的数据尝试了 Antihead 的答案,效果很好。但是当应用到我自己的数据时,它不会。它给我留下了一个空的数据框:
import collections
def fill_cells(cell):
v_max = cell['Votes']
all_dict = {}
#iterate over parties.copy()
for p in parties:
tmp_l = parties.copy()
tmp_l.remove(p)
# sample new data with equal choices
sampled = np.random.choice(tmp_l, int(v_max-cell[p]))
# transform into dictionary
c_sampled = dict(collections.Counter(sampled))
c_sampled.update({p:cell[p]})
# batch update of the dictio~nary keys
all_dict.update(
dict(zip([p+'_%s' %k[1] for k in c_sampled.keys()], c_sampled.values()))
)
return pd.Series(all_dict)
确实,具有以下数据框:
Comm Votes LPC CPC BQ
0 comm1 1315.0 2.0 424.0 572.0
1 comm2 4682.0 117.0 2053.0 1584.0
2 comm3 2397.0 2.0 40.0 192.0
3 comm4 931.0 2.0 12.0 345.0
4 comm5 842.0 47.0 209.0 76.0
... ... ... ... ... ...
1522 comm1523 23808.0 1588.0 4458.0 13147.0
1523 comm1524 639.0 40.0 126.0 40.0
1524 comm1525 10477.0 13.0 673.0 333.0
1525 comm1526 2674.0 1.0 55.0 194.0
1526 comm1527 1691.0 331.0 29.0 78.0
我有一个空数据框:
0
1
2
3
4
...
1522
1523
1524
1525
1526
【问题讨论】:
-
您能否重新表述问题以便更好地理解?你想不想计算未知社区中的一个人投票给 P1、P2、P3 的机会?
-
@Antihead 当然。例如,如果我们取 comm1(给出行)和一方 P1,我只希望每个单元格 P1_i 中的随机数为 [1,方数] 中的 i,它们的总和必须等于投票数且 P1_1 必须等于到 P1。这有意义吗?
-
我理解正确吗:对于每个单元格:您想将剩余的选票划分为:
|Votes|-P_i,到P_{j}j element from [1,2,3] where i!=j? (随机) -
@Antihead,是的! P_{j} 是随机的,表示政党 P 的票数
-
tmp_l = parties不会复制您的列表,而是引用它。你需要复制列表而不是tmp_l = parties,copy()
标签: python python-3.x dataframe random