最小值适用于Pandas数据帧和系列

2024-09-29 19:25:34 发布

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

我可以使用np.minimum和{}使用广播,例如:

a.shape = (100, 5)
b.shape = (5,)
c = np.mininum(a,b)
c.shape = (100, 5) # minumum elementwise between a and b

我该怎么做呢,但是要处理pandas DataFrameSeries对象?在

我们不能使用value属性,因为我们可能会丢失列的顺序。我想把这个订单考虑进去。在


Tags: and对象dataframepandas属性顺序valuenp
2条回答

这可能不是最有效的方法,但它可以保持列顺序。您只需将dataframeseries转换回numpy数组来执行minimum操作,将其转换回dataframe。例如:

In [43]: df=pd.DataFrame(np.random.rand(10,5))

In [44]: sr=pd.Series(np.random.rand(5))

In [45]: df
Out[45]: 
          0         1         2         3         4
0  0.435234  0.197012  0.364953  0.942068  0.657147
1  0.310736  0.721353  0.880256  0.140999  0.757069
2  0.840233  0.957006  0.785870  0.884206  0.625479
3  0.368817  0.386193  0.634408  0.895458  0.433639
4  0.804589  0.509249  0.124370  0.556714  0.895174
5  0.034010  0.519510  0.853540  0.192033  0.234513
6  0.262984  0.270159  0.673854  0.465467  0.906740
7  0.318838  0.518621  0.295384  0.596599  0.612002
8  0.804619  0.616971  0.309750  0.544413  0.013770
9  0.440933  0.857697  0.447541  0.266759  0.002859

In [46]: sr
Out[46]: 
0    0.807357
1    0.605892
2    0.328464
3    0.298340
4    0.424584
dtype: float64

In [47]: dfmin = pd.DataFrame(np.minimum(np.array(df),np.array(sr)))

In [48]: dfmin
Out[48]: 
          0         1         2         3         4
0  0.435234  0.197012  0.328464  0.298340  0.424584
1  0.310736  0.605892  0.328464  0.140999  0.424584
2  0.807357  0.605892  0.328464  0.298340  0.424584
3  0.368817  0.386193  0.328464  0.298340  0.424584
4  0.804589  0.509249  0.124370  0.298340  0.424584
5  0.034010  0.519510  0.328464  0.192033  0.234513
6  0.262984  0.270159  0.328464  0.298340  0.424584
7  0.318838  0.518621  0.295384  0.298340  0.424584
8  0.804619  0.605892  0.309750  0.298340  0.013770
9  0.440933  0.605892  0.328464  0.266759  0.002859

我不清楚你在这里要做什么,因为在纽比,它不会工作,因为形状不能被广播:

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,) 

如果尺寸已交换,则可以:

^{pr2}$

因此,对于大熊猫来说,以下方法是可行的:

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属性来完成

相关问题 更多 >

    热门问题