两个不同维度的数据帧相乘

2024-04-28 19:24:47 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有两个数据帧,一个大小(462,38),另一个(36,34)。理论上,我应该可以将它们相乘(第一个不带前两列,即462,36))并得到一个大小为(462,34)的矩阵,但是我收到了一个错误。这是我使用的脚本:

df_technique = df_binary.iloc[:,2:].mul(df_pim.values, axis=0)

df_binary是大小为(462,38)的数据帧 df_pim是大小为(36,34)的数据帧

我得到了这个错误:

ValueError: Unable to coerce to DataFrame, shape must be (462, 36): given (36, 34)

这对熊猫来说是个新手,但有点搞不清为什么这不起作用。在


Tags: to数据脚本df错误矩阵理论values
1条回答
网友
1楼 · 发布于 2024-04-28 19:24:47

让我们创建一个小玩具示例:

In [6]: import numpy as np

In [7]: import pandas as pd

In [8]: df1 = pd.DataFrame(np.random.rand(462,38))

In [9]: df2 = pd.DataFrame(np.random.rand(36,34))

In [10]: df1.iloc[:, 2:].shape
Out[10]: (462, 36)

让我们试试.mul

^{pr2}$

这是可行的,但是有很多NaN值。这是因为.mul执行数据帧的元素级乘法,并且期望数据帧的形状是(A, B) x (A, B)。在

使用df2.values时,我们可以重现您的错误,因为pandas试图从(36, 34)df2.values numpy数组中创建一个(462, 36)数据帧,但失败了:

In [13]: df1.iloc[:, 2:].mul(df2.values, axis=0)
                                     -
ValueError                                Traceback (most recent call last)
<ipython-input-13-e33efa65ac07> in <module>()
  > 1 df1.iloc[:, 2:].mul(df2.values, axis=0)

~/miniconda3/envs/so_question/lib/python3.6/site-packages/pandas/core/ops.py in f(self, other, axis, level, fill_value)
   1515     def f(self, other, axis=default_axis, level=None, fill_value=None):
   1516 
-> 1517         other = _align_method_FRAME(self, other, axis)
   1518 
   1519         if isinstance(other, ABCDataFrame):  # Another DataFrame

~/miniconda3/envs/so_question/lib/python3.6/site-packages/pandas/core/ops.py in _align_method_FRAME(left, right, axis)
   1438                                  "must be {req_shape}: given {given_shape}"
   1439                                  .format(req_shape=left.shape,
-> 1440                                          given_shape=right.shape))
   1441 
   1442             right = left._constructor(right, index=left.index,

ValueError: Unable to coerce to DataFrame, shape must be (462, 36): given (36, 34)

从你的描述来看,你打算用.dot来代替(M, N) x (N, P) = (M, P)而不是{}:

In [17]: df1.iloc[:, 2:].dot(df2.values)
Out[17]: 
            0          1          2          3          4     ...            29        30         31         32        33
0     9.971979   6.981089   8.694962   8.276216   8.351839    ...      8.480659  8.414970   8.240727   9.672677  7.565088
1     9.410364   8.590233   8.195569   9.140421   8.921937    ...      8.570695  7.076403  10.040924   8.267829  8.325542
2    11.095329   9.576891   9.809456  10.026001  10.696129    ...      9.234288  8.387796  11.306832  10.665649  8.983012
3    10.195718   8.550438   9.006633   9.628953   9.156615    ...      8.921587  7.133165  10.254906   9.561331  8.968170
4    10.169194   9.450612   8.424815   8.513062   8.537338    ...      8.358502  8.129391   9.731362   9.567829  8.041028
5     8.699785   7.506038   7.247128   8.637461   7.905982    ...      7.635597  6.348187   8.452615   7.902099  7.091182
6     9.231585   7.692058   7.744410   8.488541   8.450400    ...      7.932535  6.753257   8.688555   8.658745  7.056945
7     9.622983   8.674558   6.987342   8.077567   7.955983    ...      8.264570  6.685770   9.142239   9.154905  7.833893
8    11.309864   9.142239   8.419535   8.680210   9.712205    ...      8.717705  7.664762  10.649243   9.459739  8.912268
9    11.334008   8.958880   8.296706   9.420058   9.756113    ...      9.399543  7.366976  10.139639  10.154981  8.350516
10   11.181974  10.031747   9.294010  11.156024  10.066875    ...      9.613627  8.053175  11.583264  10.818940  9.330073
11    9.980280   8.311433   7.501879   8.494200   7.832875    ...      8.457131  7.320341   9.921714   8.975098  8.443220
12    9.740919   8.728053   7.673338   8.007482   9.291362    ...      8.483178  6.957094   9.554731   8.977761  7.974371
13   11.167700  10.239565  11.016358  11.550972  10.918983    ...      9.373120  9.645397  12.572644  11.505732  9.894803
14   10.680348   8.988366   9.009413   8.823574   9.424792    ...      8.959995  8.185815   9.940674   9.927982  8.334847
15   11.830672  10.053389   9.302099  10.251534   9.440460    ...     10.139816  9.478568  10.860152  11.184854  8.287742
16    9.339399   8.916890   7.187363   8.922513   8.426588    ...      7.944471  7.323630   9.814955   8.656069  7.053032
17   11.143226   8.808918   9.178566   8.847451   9.175966    ...      9.158147  7.899361   9.656798   9.994228  8.879669
18    9.889310   8.284430   7.786766   8.221434   8.435520    ...      7.373773  6.921359   8.685328   9.677998  8.130656
19   11.021434   9.315304   7.878728   9.712656   9.381570    ...      8.506453  7.138838   9.409518  10.872490  8.920906
20    8.465932   7.376756   7.873896   8.216718   7.981726    ...      7.606467  6.071998   8.783477   8.713840  6.978514
21   10.044225   8.490197   7.624420   8.063905   8.485409    ...      8.590962  6.953432  10.188383   8.691053  7.867377
22   10.055971   8.596160   8.004438   8.487240   8.243792    ...      8.232528  6.805463   9.322255  10.063531  7.578448
23   11.260788  10.345866   8.547624   9.151512   9.672593    ...      8.784488  8.244342  10.440683  11.396178  9.597543
24   11.289788   9.181376   9.678903   9.205845  10.020440    ...      9.945561  8.558815  10.285292  10.364807  8.624407
25    9.735467   7.731079   7.858442   8.098820   7.329694    ...      8.992903  7.179417   8.045042   8.872422  7.592073
26    8.951750   7.260067   8.087883   8.625076   7.796546    ...      8.368493  6.549368   8.282185   8.082291  7.088356
27    8.493655   7.288378   7.554746   7.793805   7.555760    ...      7.146606  5.828258   7.744346   8.613483  7.455887
28    9.915362   8.161043   9.110862   9.147840   8.513224    ...      8.676128  7.878927   8.752682  10.031502  8.360679
29   11.225115   8.611056   9.385648   9.472114   9.581927    ...      8.964314  8.321400  10.043396  10.177277  8.422191
..         ...        ...        ...        ...        ...    ...           ...       ...        ...        ...       ...
432   7.627470   5.734383   6.626045   6.156603   7.429465    ...      6.461749  5.636417   6.518002   6.925516  5.183975
433  11.643077  10.056751   9.313494  10.320367   9.455274    ...      9.992466  8.058518  11.759124  11.398521  9.627579
434   9.011228   8.956598   7.391039   8.274706   8.709996    ...      7.739301  7.270689   8.638466   8.497952  7.203451
435   9.265928   7.693394   8.228558   7.400204   8.619893    ...      7.547710  7.003936   8.839432   9.568429  7.334440
436   9.920282   9.012674   8.142472   9.055654   9.253443    ...      8.413467  6.989122   8.762788   9.328164  7.850035
437   9.944510   7.895858   7.797087   8.212704   8.687516    ...      8.068419  6.169651   8.120617   9.299318  6.777742
438  10.494847   8.672840   9.410736   9.288373   9.146171    ...      9.597911  7.760452  10.141617  10.452433  9.498194
439   9.867494   8.142811   8.214623   8.060762   9.040205    ...      8.427159  7.106905  10.524450   9.292304  8.472231
440  10.130309   8.778968   8.098571   9.036090   9.253507    ...      8.885713  7.310063  10.105144   9.545208  8.807030
441  10.251218   7.729740   7.370521   8.384513   6.958638    ...      8.240842  6.977351   9.267558   9.143776  7.798779
442   8.748951   7.197680   7.028517   7.348912   7.809581    ...      7.896705  6.635679   6.819049   8.045874  6.039097
443  11.703053   9.065089   8.927836   9.542447   9.241294    ...      9.779255  8.794765  10.497803   9.763386  8.408181
444  11.433442  10.707882  10.134235  11.019976  10.722692    ...      9.853438  8.556864  11.099727  11.881496  9.348087
445   8.794981   7.614707   7.855584   8.671275   7.436318    ...      7.282331  6.869884   8.410838   8.980563  7.358201
446   8.735594   7.520451   7.336556   8.235567   7.554697    ...      6.827973  6.302846   8.694671   7.736256  7.746888
447   8.930933   7.674217   7.478743   8.077684   7.300099    ...      7.524244  6.481388   9.119867   8.043605  6.514627
448   9.701053   7.237276   8.207318   8.704792   8.145399    ...      8.176370  7.334896   7.827332   9.504545  8.012765
449   9.932439   8.175081   7.803909   9.206564   9.782949    ...      9.125961  7.279960   8.518182   9.648755  7.742197
450   9.365815   8.567567   7.579989   8.670692   9.055934    ...      7.980583  6.721377   9.921531   9.309550  8.125973
451   9.075117   8.108147   8.607631   8.185085   7.895500    ...      8.036308  6.766023   9.057757   9.009128  7.459431
452   9.059566   8.659570   7.626333   9.360041   8.226206    ...      7.634966  7.739625   8.960765   8.781221  7.528786
453   8.432637   7.678105   7.610083   7.640916   8.305286    ...      6.888020  6.553264   8.646732   8.628493  7.721341
454   9.055456   7.311839   7.413838   7.267197   7.253457    ...      7.585661  7.061526   8.355587   8.697914  7.646652
455   9.516074   8.071045   8.149921   9.263839   8.287315    ...      7.421049  6.959464   9.453780   9.958168  8.350330
456  10.070524   8.916685   9.093280   9.447184   9.086083    ...      9.234514  7.800037  10.031802   9.686283  9.196937
457  10.726476   8.270430   7.704192   8.026725   9.541432    ...      8.338713  6.688143   8.785184  10.426090  8.658769
458   9.751500   7.310215   8.136909   8.312466   7.316233    ...      7.874102  7.796568   9.434560   7.996315  8.094219
459   9.501868   8.039517   7.932875   8.385449   8.937402    ...      8.569393  6.948936   7.823537   9.030505  7.035568
460   9.476510   7.735094   7.266457   9.046955   8.358792    ...      8.635297  7.403172   8.382819   8.930228  6.963464
461  10.638806   9.091559   8.946520   9.440301  10.823085    ...      8.734041  7.767178   9.745443   9.859176  8.198679

[462 rows x 34 columns]

相关问题 更多 >