您可以使用np.clip
夾上限和下限。
df['Battery capacity'] = np.clip(df['total_load'].cumsum() + 5200,-np.inf,5200)
或者作爲@jezrael指出熊貓系列具有clip
方法:
df['Battery capacity'] = (df['total_load'].cumsum() + 5200).clip(-np.inf,5200)
輸出:
Battery capacity total_load
2016-01-01 00:00:00 4755.0000 -445.0000
2016-01-01 01:00:00 4375.0000 -380.0000
2016-01-01 02:00:00 4025.0000 -350.0000
2016-01-01 03:00:00 3685.0000 -340.0000
2016-01-01 04:00:00 2955.4500 -729.5500
2016-01-01 05:00:00 1870.4500 -1085.0000
2016-01-01 06:00:00 879.1500 -991.3000
2016-01-01 07:00:00 -2555.8333 -3434.9833
2016-01-01 08:00:00 -1952.7503 603.0830
2016-01-01 09:00:00 -864.7503 1088.0000
2016-01-01 10:00:00 1155.2497 2020.0000
2016-01-01 11:00:00 2336.2497 1181.0000
2016-01-01 12:00:00 4486.2497 2150.0000
2016-01-01 13:00:00 5200.0000 1200.8330
2016-01-01 14:00:00 5200.0000 1980.0000
2016-01-01 15:00:00 5200.0000 -221.2667
現在,如果你不希望值,低於去零替換-np.inf爲0.
Battery capacity total_load
2016-01-01 00:00:00 4755.0000 -445.0000
2016-01-01 01:00:00 4375.0000 -380.0000
2016-01-01 02:00:00 4025.0000 -350.0000
2016-01-01 03:00:00 3685.0000 -340.0000
2016-01-01 04:00:00 2955.4500 -729.5500
2016-01-01 05:00:00 1870.4500 -1085.0000
2016-01-01 06:00:00 879.1500 -991.3000
2016-01-01 07:00:00 0.0000 -3434.9833
2016-01-01 08:00:00 0.0000 603.0830
2016-01-01 09:00:00 0.0000 1088.0000
2016-01-01 10:00:00 1155.2497 2020.0000
2016-01-01 11:00:00 2336.2497 1181.0000
2016-01-01 12:00:00 4486.2497 2150.0000
2016-01-01 13:00:00 5200.0000 1200.8330
2016-01-01 14:00:00 5200.0000 1980.0000
2016-01-01 15:00:00 5200.0000 -221.2667
也許是p也可以使用['clip'](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.clip.html) – jezrael
@jezrael另一種我不知道的方法存在於熊貓庫。謝謝。 –