2017-08-16 97 views
0

我有一個熊貓df和df['Battery capacity'] = df['total_load'].cumsum() + 5200 我用「battery_capacity」中的值減去「total_load」中的值。熊貓 - 破壞熊貓數據幀中cumsum()代碼的加/減

enter image description here

所以,現在我想的東西添加到我的代碼,打破了添加/在一定值減去。例如,我不想要比5200更高的值。因此,假設在13:00:00,加起來應該停在5200. 我怎麼能在我的代碼中實現?斯科特波士頓提出了一個if語句,但你會如何使用我的代碼df['Battery capacity'] = df['total_load'].cumsum(if battery capacity = 5200, then stop adding) + 5200 我應該嘗試寫一個函數嗎?

輸出應該是這樣的:

time    total_load battery capacity 
2016-06-01 12:00:00 2150  4487.7 
2016-06-01 13:00:00 1200  5688 (but should stop at 5200) 
2016-06-01 14:00:00 1980  5200 (don't actually add values now because we are still at 5200) 

回答

2

您可以使用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 
+1

也許是p也可以使用['clip'](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.clip.html) – jezrael

+1

@jezrael另一種我不知道的方法存在於熊貓庫。謝謝。 –