在python的其他datapandas框架中创建pandas的其他子列表

2024-05-18 04:26:50 发布

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

那么如何从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数据帧。在


Tags: of数据inpandasindexisvaluerange
1条回答
网友
1楼 · 发布于 2024-05-18 04:26:50

如果我能理解你,可能会像:

>>> 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),这些特性可能不成立。在

相关问题 更多 >