这我不清楚你想要什么这里作为numpy的,它不会工作的形状不能播:
In [188]:
a = np.random.randn(100,5)
b = np.random.randn(100)
c = np.minimum(a,b)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-188-f59fe6dbb41e> in <module>()
1 a = np.random.randn(100,5)
2 b = np.random.randn(100)
----> 3 c = np.minimum(a,b)
ValueError: operands could not be broadcast together with shapes (100,5) (100,)
如果尺寸被交换,然后它的工作原理:
In [193]:
a = np.random.randn(100,5)
b = np.random.randn(100)
c = np.minimum(a.T,b)
c.shape
Out[193]:
(5, 100)
所以对大熊猫以下将工作:
In [191]:
s = pd.Series(np.random.randn(100))
df = pd.DataFrame(np.random.randn(100,3))
np.minimum(df.T,s.values)
Out[191]:
0 1 2 3 4 5 6 \
0 -0.462166 -0.753243 -0.857485 -0.783888 -1.058906 -1.782304 -2.866326
1 0.586516 -0.735980 -0.857485 -1.005976 -1.015092 -1.782304 -2.866326
2 -1.689027 -0.735980 -1.102960 -0.283301 -1.015092 -1.782304 -2.866326
7 8 9 ... 90 91 92 \
0 -0.967473 -0.824018 -0.633347 ... 0.022141 -0.794049 -0.522190
1 -0.967473 -0.824018 0.066065 ... -0.225902 -0.794049 -0.694794
2 -0.967473 -0.824018 0.066065 ... 0.022141 -0.794049 0.278394
93 94 95 96 97 98 99
0 -0.365531 -0.330756 -1.495789 -1.375226 -1.097268 -1.395099 -1.971968
1 -1.805734 -0.330756 -1.495789 -1.375226 -1.097268 -1.395099 -0.543660
2 -1.328497 -0.330756 -1.495789 -1.375226 -1.097268 -1.395099 -0.104600
[3 rows x 100 columns]
所以你需要调换df(如果需要)才能使广播正常工作,那么该系列需要展平为一维阵列,在这种情况下可以通过调用.values
属性
我知道。我不想要最小的数据帧元素。我希望将数据帧的最小值与系列相比 – EntrustName
好的,我已经编辑了我的答案,希望对您有所帮助。 – tom
是的,其实它的工作原理非常感谢,但是如果你有命名列(数据框)和索引(系列),你必须对它们进行排序,然后将索引和列设置为最新的DataFrame('dfmin')。我不喜欢这样做,这就是为什么我寻找一个更加公平的足够友好的解决方案 – EntrustName