2014-02-07 31 views
0

那麼,如何從A和B到C:創建子表的最大的在基於其他列蟒蛇大熊貓據幀

Index A  B  C 
0  0  2  0  //0 because A is 0 
1  0  4  0  //0 because A is 0 
2  1  1  1  //max value of B in range(2, 2) inclusive 
3  1  3  3  //max value of B in range(2, 3) inclusive 
4  1  6  6  //max value of B in range(2, 4) inclusive 
5  1  4  6  //max value of B in range(2, 5) inclusive 
6  0  1  0  //0 because A is 0 
7  1  2  2  //max value of B in range(7, 7) inclusive 
8  1  1  2  //max value of B in range(7, 8) inclusive 
9  0  9  0  //0 because A is 0 

注意,這些都是熊貓DataFrames。

回答

2

假設我理解你,也許是這樣的:

>>> df 
     A B 
Index  
0  0 2 
1  0 4 
2  1 1 
3  1 3 
4  1 6 
5  1 4 
6  0 1 
7  1 2 
8  1 1 
9  0 9 

[10 rows x 2 columns] 
>>> df["C"] = (df.A * df.B).groupby((df.A == 0).cumsum()).cummax() 
>>> df 
     A B C 
Index   
0  0 2 0 
1  0 4 0 
2  1 1 1 
3  1 3 3 
4  1 6 6 
5  1 4 6 
6  0 1 0 
7  1 2 2 
8  1 1 2 
9  0 9 0 

[10 rows x 3 columns] 

,但你的問題感到得以確認給我,我可以依靠數據的功能(如一個始終爲0或1)這可能不成立。

+0

在我的情況下,A將始終爲0或1,並且好像您的方法應該起作用,所以謝謝! – Nezo