python – 将两个不同数据帧中每行的值相乘

python – 将两个不同数据帧中每行的值相乘,第1张

概述我正在构建遗传算法以在 python中进行特征选择.我从我的数据中提取了特征,然后我分成了两个数据帧,“训练”和“测试”数据帧. 如何将“填充”数据框(每个单独)和“训练”数据框中的每一行的值复用? ‘火车’数据帧: feature0 feature1 feature2 feature3 feature4 feature50 18.279579 -3.921346 13 我正在构建遗传算法以在 python中进行特征选择.我从我的数据中提取了特征,然后我分成了两个数据帧,“训练”和“测试”数据帧.
如何将“填充”数据框(每个单独)和“训练”数据框中的每一行的值复用?

‘火车’数据帧:

feature0   feature1   feature2   feature3   feature4   feature50  18.279579  -3.921346  13.611829  -7.250185 -11.773605 -18.265003   1  17.899545 -15.503942  -0.741729  -0.053619  -6.734652   4.398419   4  16.432750 -22.490190  -4.611659 -15.247781 -13.941488  -2.433374   5  15.905368  -4.812785  18.291712   3.742221   3.631887  -1.074326   6  16.991823 -15.946251   8.299577   8.057511   8.057510  -1.482333

‘人口’数据框:

0     1     2     3     4     5     0     1     1     0     0     0     1     1     0     1     0     1     0     0     2     0     0     0     0     0     1     3     0     0     1     0     1     1

将’population’中的每一行乘以’train’中的所有行.
结果如下:

1)从人口第1行:

feature0   feature1   feature2   feature3   feature4   feature50  18.279579  -3.921346          0          0          0 -18.265003   1  17.899545 -15.503942          0          0          0   4.398419   4  16.432750 -22.490190          0          0          0  -2.433374   5  15.905368  -4.812785          0          0          0  -1.074326   6  16.991823 -15.946251          0          0          0  -1.482333

2)从人口第2行:

feature0   feature1   feature2   feature3   feature4   feature50          0  -3.921346          0  -7.250185          0          01          0 -15.503942          0  -0.053619          0          0   4          0 -22.490190          0 -15.247781          0          0   5          0  -4.812785          0   3.742221          0          0   6          0 -15.946251          0   8.057511          0          0

等等…

解决方法 如果需要循环(如果大数据则缓慢):

for i,x in population.iterrows():    print (train * x.values)    feature0   feature1  feature2  feature3  feature4   feature50  18.279579  -3.921346       0.0      -0.0      -0.0 -18.2650031  17.899545 -15.503942      -0.0      -0.0      -0.0   4.3984194  16.432750 -22.490190      -0.0      -0.0      -0.0  -2.4333745  15.905368  -4.812785       0.0       0.0       0.0  -1.0743266  16.991823 -15.946251       0.0       0.0       0.0  -1.482333   feature0   feature1  feature2   feature3  feature4  feature50       0.0  -3.921346       0.0  -7.250185      -0.0      -0.01       0.0 -15.503942      -0.0  -0.053619      -0.0       0.04       0.0 -22.490190      -0.0 -15.247781      -0.0      -0.05       0.0  -4.812785       0.0   3.742221       0.0      -0.06       0.0 -15.946251       0.0   8.057511       0.0      -0.0   feature0  feature1  feature2  feature3  feature4   feature50       0.0      -0.0       0.0      -0.0      -0.0 -18.2650031       0.0      -0.0      -0.0      -0.0      -0.0   4.3984194       0.0      -0.0      -0.0      -0.0      -0.0  -2.4333745       0.0      -0.0       0.0       0.0       0.0  -1.0743266       0.0      -0.0       0.0       0.0       0.0  -1.482333   feature0  feature1   feature2  feature3   feature4   feature50       0.0      -0.0  13.611829      -0.0 -11.773605 -18.2650031       0.0      -0.0  -0.741729      -0.0  -6.734652   4.3984194       0.0      -0.0  -4.611659      -0.0 -13.941488  -2.4333745       0.0      -0.0  18.291712       0.0   3.631887  -1.0743266       0.0      -0.0   8.299577       0.0   8.057510  -1.482333

或者每行分开:

print (train * population.values[0])    feature0   feature1  feature2  feature3  feature4   feature50  18.279579  -3.921346       0.0      -0.0      -0.0 -18.2650031  17.899545 -15.503942      -0.0      -0.0      -0.0   4.3984194  16.432750 -22.490190      -0.0      -0.0      -0.0  -2.4333745  15.905368  -4.812785       0.0       0.0       0.0  -1.0743266  16.991823 -15.946251       0.0       0.0       0.0  -1.482333

或者对于MultiIndex DataFrame:

d = pd.concat([train * population.values[i] for i in range(population.shape[0])],keys=population.index.toList())print (d)      feature0   feature1   feature2   feature3   feature4   feature50 0  18.279579  -3.921346   0.000000  -0.000000  -0.000000 -18.265003  1  17.899545 -15.503942  -0.000000  -0.000000  -0.000000   4.398419  4  16.432750 -22.490190  -0.000000  -0.000000  -0.000000  -2.433374  5  15.905368  -4.812785   0.000000   0.000000   0.000000  -1.074326  6  16.991823 -15.946251   0.000000   0.000000   0.000000  -1.4823331 0   0.000000  -3.921346   0.000000  -7.250185  -0.000000  -0.000000  1   0.000000 -15.503942  -0.000000  -0.053619  -0.000000   0.000000  4   0.000000 -22.490190  -0.000000 -15.247781  -0.000000  -0.000000  5   0.000000  -4.812785   0.000000   3.742221   0.000000  -0.000000  6   0.000000 -15.946251   0.000000   8.057511   0.000000  -0.0000002 0   0.000000  -0.000000   0.000000  -0.000000  -0.000000 -18.265003  1   0.000000  -0.000000  -0.000000  -0.000000  -0.000000   4.398419  4   0.000000  -0.000000  -0.000000  -0.000000  -0.000000  -2.433374  5   0.000000  -0.000000   0.000000   0.000000   0.000000  -1.074326  6   0.000000  -0.000000   0.000000   0.000000   0.000000  -1.4823333 0   0.000000  -0.000000  13.611829  -0.000000 -11.773605 -18.265003  1   0.000000  -0.000000  -0.741729  -0.000000  -6.734652   4.398419  4   0.000000  -0.000000  -4.611659  -0.000000 -13.941488  -2.433374  5   0.000000  -0.000000  18.291712   0.000000   3.631887  -1.074326  6   0.000000  -0.000000   8.299577   0.000000   8.057510  -1.482333

并按xs选择:

print (d.xs(0))    feature0   feature1  feature2  feature3  feature4   feature50  18.279579  -3.921346       0.0      -0.0      -0.0 -18.2650031  17.899545 -15.503942      -0.0      -0.0      -0.0   4.3984194  16.432750 -22.490190      -0.0      -0.0      -0.0  -2.4333745  15.905368  -4.812785       0.0       0.0       0.0  -1.0743266  16.991823 -15.946251       0.0       0.0       0.0  -1.482333
总结

以上是内存溢出为你收集整理的python – 将两个不同数据帧中每行的值相乘全部内容,希望文章能够帮你解决python – 将两个不同数据帧中每行的值相乘所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1194065.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存