首页 > 解决方案 > 如何为每个具有比例值的元素列表创建成对值矩阵?

问题描述

我有一个文件,其中包含一个名称列表,每个名称都有一个比例值。我需要生成一个矩阵,即每对名称的值的乘积。

import pandas as pd
import numpy as np
df = pd.read_csv('candegree.csv', usecols=[0, 5], nrows=26)  # if using the csv file
df.head()

数据candegree.csv

    Candidate  Relations
       bennet       0.52
        biden       0.92
       blasio       0.66
       booker       0.84
      bullock       0.79
    buttigieg       0.75
       castro       0.74
      delaney       0.59
      gabbard       0.77
   gillibrand       0.78
       harris       0.88
 hickenlooper       0.79
       inslee       0.70
    klobuchar       0.77
      moulton       0.71
      orourke       0.80
         ryan       0.79
      sanders       0.88
       steyer       0.73
     swalwell       0.81
        trump       0.91
        walsh       0.69
       warren       0.86
         weld       0.53
   williamson       0.69
         yang       0.81

标签: python

解决方案


以下代码生成每对名称的乘积。

  • 对于名称和自身的乘积的情况,该值设置为np.NaN
    • df[name].loc[name] = np.NaN如果您想要该产品,请删除
    • type(np.NaN)= float,因此,填充np.NaN确保能够执行按列计算,而不会遇到问题type
    • 或者,设置df[name].loc[name] = None, 也应该工作,这排除了使用的必要性numpy
  • 设置Candidateindex,然后用于.loc查找Relations个人名称的系数,该系数可用于逐列乘法。
import pandas as pd
import numpy as np


df = pd.read_csv('candegree.csv', usecols=[0, 5], nrows=26)

df.set_index('Candidate', inplace=True)

for name in df.index:
    df[name] = df.Relations * df.Relations.loc[name]
    df[name].loc[name] = np.NaN

df.reset_index(inplace=True)

输出:

    Candidate  Relations  bennet   biden  blasio  booker  bullock  buttigieg  castro  delaney  gabbard  gillibrand  harris  hickenlooper  inslee  klobuchar  moulton  orourke    ryan  sanders  steyer  swalwell   trump   walsh  warren    weld  williamson    yang
       bennet       0.52     NaN  0.4784  0.3432  0.4368   0.4108     0.3900  0.3848   0.3068   0.4004      0.4056  0.4576        0.4108   0.364     0.4004   0.3692    0.416  0.4108   0.4576  0.3796    0.4212  0.4732  0.3588  0.4472  0.2756      0.3588  0.4212
        biden       0.92  0.4784     NaN  0.6072  0.7728   0.7268     0.6900  0.6808   0.5428   0.7084      0.7176  0.8096        0.7268   0.644     0.7084   0.6532    0.736  0.7268   0.8096  0.6716    0.7452  0.8372  0.6348  0.7912  0.4876      0.6348  0.7452
       blasio       0.66  0.3432  0.6072     NaN  0.5544   0.5214     0.4950  0.4884   0.3894   0.5082      0.5148  0.5808        0.5214   0.462     0.5082   0.4686    0.528  0.5214   0.5808  0.4818    0.5346  0.6006  0.4554  0.5676  0.3498      0.4554  0.5346
       booker       0.84  0.4368  0.7728  0.5544     NaN   0.6636     0.6300  0.6216   0.4956   0.6468      0.6552  0.7392        0.6636   0.588     0.6468   0.5964    0.672  0.6636   0.7392  0.6132    0.6804  0.7644  0.5796  0.7224  0.4452      0.5796  0.6804
      bullock       0.79  0.4108  0.7268  0.5214  0.6636      NaN     0.5925  0.5846   0.4661   0.6083      0.6162  0.6952        0.6241   0.553     0.6083   0.5609    0.632  0.6241   0.6952  0.5767    0.6399  0.7189  0.5451  0.6794  0.4187      0.5451  0.6399
    buttigieg       0.75  0.3900  0.6900  0.4950  0.6300   0.5925        NaN  0.5550   0.4425   0.5775      0.5850  0.6600        0.5925   0.525     0.5775   0.5325    0.600  0.5925   0.6600  0.5475    0.6075  0.6825  0.5175  0.6450  0.3975      0.5175  0.6075
       castro       0.74  0.3848  0.6808  0.4884  0.6216   0.5846     0.5550     NaN   0.4366   0.5698      0.5772  0.6512        0.5846   0.518     0.5698   0.5254    0.592  0.5846   0.6512  0.5402    0.5994  0.6734  0.5106  0.6364  0.3922      0.5106  0.5994
      delaney       0.59  0.3068  0.5428  0.3894  0.4956   0.4661     0.4425  0.4366      NaN   0.4543      0.4602  0.5192        0.4661   0.413     0.4543   0.4189    0.472  0.4661   0.5192  0.4307    0.4779  0.5369  0.4071  0.5074  0.3127      0.4071  0.4779
      gabbard       0.77  0.4004  0.7084  0.5082  0.6468   0.6083     0.5775  0.5698   0.4543      NaN      0.6006  0.6776        0.6083   0.539     0.5929   0.5467    0.616  0.6083   0.6776  0.5621    0.6237  0.7007  0.5313  0.6622  0.4081      0.5313  0.6237
   gillibrand       0.78  0.4056  0.7176  0.5148  0.6552   0.6162     0.5850  0.5772   0.4602   0.6006         NaN  0.6864        0.6162   0.546     0.6006   0.5538    0.624  0.6162   0.6864  0.5694    0.6318  0.7098  0.5382  0.6708  0.4134      0.5382  0.6318
       harris       0.88  0.4576  0.8096  0.5808  0.7392   0.6952     0.6600  0.6512   0.5192   0.6776      0.6864     NaN        0.6952   0.616     0.6776   0.6248    0.704  0.6952   0.7744  0.6424    0.7128  0.8008  0.6072  0.7568  0.4664      0.6072  0.7128
 hickenlooper       0.79  0.4108  0.7268  0.5214  0.6636   0.6241     0.5925  0.5846   0.4661   0.6083      0.6162  0.6952           NaN   0.553     0.6083   0.5609    0.632  0.6241   0.6952  0.5767    0.6399  0.7189  0.5451  0.6794  0.4187      0.5451  0.6399
       inslee       0.70  0.3640  0.6440  0.4620  0.5880   0.5530     0.5250  0.5180   0.4130   0.5390      0.5460  0.6160        0.5530     NaN     0.5390   0.4970    0.560  0.5530   0.6160  0.5110    0.5670  0.6370  0.4830  0.6020  0.3710      0.4830  0.5670
    klobuchar       0.77  0.4004  0.7084  0.5082  0.6468   0.6083     0.5775  0.5698   0.4543   0.5929      0.6006  0.6776        0.6083   0.539        NaN   0.5467    0.616  0.6083   0.6776  0.5621    0.6237  0.7007  0.5313  0.6622  0.4081      0.5313  0.6237
      moulton       0.71  0.3692  0.6532  0.4686  0.5964   0.5609     0.5325  0.5254   0.4189   0.5467      0.5538  0.6248        0.5609   0.497     0.5467      NaN    0.568  0.5609   0.6248  0.5183    0.5751  0.6461  0.4899  0.6106  0.3763      0.4899  0.5751
      orourke       0.80  0.4160  0.7360  0.5280  0.6720   0.6320     0.6000  0.5920   0.4720   0.6160      0.6240  0.7040        0.6320   0.560     0.6160   0.5680      NaN  0.6320   0.7040  0.5840    0.6480  0.7280  0.5520  0.6880  0.4240      0.5520  0.6480
         ryan       0.79  0.4108  0.7268  0.5214  0.6636   0.6241     0.5925  0.5846   0.4661   0.6083      0.6162  0.6952        0.6241   0.553     0.6083   0.5609    0.632     NaN   0.6952  0.5767    0.6399  0.7189  0.5451  0.6794  0.4187      0.5451  0.6399
      sanders       0.88  0.4576  0.8096  0.5808  0.7392   0.6952     0.6600  0.6512   0.5192   0.6776      0.6864  0.7744        0.6952   0.616     0.6776   0.6248    0.704  0.6952      NaN  0.6424    0.7128  0.8008  0.6072  0.7568  0.4664      0.6072  0.7128
       steyer       0.73  0.3796  0.6716  0.4818  0.6132   0.5767     0.5475  0.5402   0.4307   0.5621      0.5694  0.6424        0.5767   0.511     0.5621   0.5183    0.584  0.5767   0.6424     NaN    0.5913  0.6643  0.5037  0.6278  0.3869      0.5037  0.5913
     swalwell       0.81  0.4212  0.7452  0.5346  0.6804   0.6399     0.6075  0.5994   0.4779   0.6237      0.6318  0.7128        0.6399   0.567     0.6237   0.5751    0.648  0.6399   0.7128  0.5913       NaN  0.7371  0.5589  0.6966  0.4293      0.5589  0.6561
        trump       0.91  0.4732  0.8372  0.6006  0.7644   0.7189     0.6825  0.6734   0.5369   0.7007      0.7098  0.8008        0.7189   0.637     0.7007   0.6461    0.728  0.7189   0.8008  0.6643    0.7371     NaN  0.6279  0.7826  0.4823      0.6279  0.7371
        walsh       0.69  0.3588  0.6348  0.4554  0.5796   0.5451     0.5175  0.5106   0.4071   0.5313      0.5382  0.6072        0.5451   0.483     0.5313   0.4899    0.552  0.5451   0.6072  0.5037    0.5589  0.6279     NaN  0.5934  0.3657      0.4761  0.5589
       warren       0.86  0.4472  0.7912  0.5676  0.7224   0.6794     0.6450  0.6364   0.5074   0.6622      0.6708  0.7568        0.6794   0.602     0.6622   0.6106    0.688  0.6794   0.7568  0.6278    0.6966  0.7826  0.5934     NaN  0.4558      0.5934  0.6966
         weld       0.53  0.2756  0.4876  0.3498  0.4452   0.4187     0.3975  0.3922   0.3127   0.4081      0.4134  0.4664        0.4187   0.371     0.4081   0.3763    0.424  0.4187   0.4664  0.3869    0.4293  0.4823  0.3657  0.4558     NaN      0.3657  0.4293
   williamson       0.69  0.3588  0.6348  0.4554  0.5796   0.5451     0.5175  0.5106   0.4071   0.5313      0.5382  0.6072        0.5451   0.483     0.5313   0.4899    0.552  0.5451   0.6072  0.5037    0.5589  0.6279  0.4761  0.5934  0.3657         NaN  0.5589
         yang       0.81  0.4212  0.7452  0.5346  0.6804   0.6399     0.6075  0.5994   0.4779   0.6237      0.6318  0.7128        0.6399   0.567     0.6237   0.5751    0.648  0.6399   0.7128  0.5913    0.6561  0.7371  0.5589  0.6966  0.4293      0.5589     NaN

推荐阅读