2015-08-28 58 views
0

我可以用广播使用np.minimumnp.maximum如:numpy.minimum应用于大熊猫数据帧和系列

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

我怎么会做这样的事情,但与大熊猫DataFrameSeries对象的工作?

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

回答

0

这可能不是最有效的方式,但它会保留您的列顺序。您只需将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 
+0

我知道。我不想要最小的数据帧元素。我希望将数据帧的最小值与系列相比 – EntrustName

+0

好的,我已经编辑了我的答案,希望对您有所帮助。 – tom

+0

是的,其实它的工作原理非常感谢,但是如果你有命名列(数据框)和索引(系列),你必须对它们进行排序,然后将索引和列设置为最新的DataFrame('dfmin')。我不喜欢这样做,这就是为什么我寻找一个更加公平的足够友好的解决方案 – EntrustName

0

这我不清楚你想要什么这里作为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属性

+0

是的,这是第二种情况。我在编辑我的问题。但是你的答案的问题是,如果你使用'value'属性,你不能考虑数据框的列顺序和系列列的顺序。所以如果你认为's'和'df'的顺序不一样,你会得到一个错误的结果 – EntrustName

+0

我不明白你最后的陈述,一个系列没有列它是一维数组 – EdChum

+0

是数据框的列,系列指数。如果数据框的列与系列的索引之间的顺序不同,则使用“值”属性 – EntrustName