這我不清楚你想要什麼這裏作爲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