基于其他列在python pandas数据帧中创建max的子列表

时间:2014-02-07 14:58:05

标签: python pandas max dataframe

那么如何从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

请注意,这些是Pandas DataFrames。

1 个答案:

答案 0 :(得分: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]

但你的问题对我来说并不明确,我可能依赖于数据的特征(例如A总是0或1),这可能不适用。