2017-10-12 83 views
0
x["next_dtScraped"] = 
if x["away_team"] == x["away_team"].shift(-1): 
    x["dtScraped"].shift(-1) 
else: 
    None 

條件所以基本上我想創造一個它會返回一個列的下一行,但只有當另一columns'row等於列的下一行一列。由於語法錯誤,上面的代碼不起作用。我不確定這是否應該走。如果大熊貓

+--------+---------------+-------------------+---------------------+---------------------+ 
|  | home_team |  away_team  |  dtScraped  | next_dtScraped | 
+--------+---------------+-------------------+---------------------+---------------------+ 
| 81965 | APOEL Nicosia | Tottenham Hotspur | 2017-09-26 17:40:48 | 2017-09-26 17:54:38 | 
| 76817 | APOEL Nicosia | Tottenham Hotspur | 2017-09-26 17:54:38 | 2017-09-26 17:56:05 | 
| 236234 | APOEL Nicosia | Tottenham Hotspur | 2017-09-26 17:56:05 | 2017-09-26 18:04:43 | 
| 192767 | APOEL Nicosia | Tottenham Hotspur | 2017-09-26 18:04:43 | 2017-09-26 18:08:38 | 
| 13448 | APOEL Nicosia | Tottenham Hotspur | 2017-09-26 18:08:38 | 2017-09-26 18:17:56 | 
| 38306 | APOEL Nicosia | Tottenham Hotspur | 2017-09-26 18:17:56 | 2017-09-26 18:23:14 | 
| 106907 | APOEL Nicosia | Tottenham Hotspur | 2017-09-26 18:23:14 | 2017-09-26 18:36:36 | 
| 235751 | APOEL Nicosia | Tottenham Hotspur | 2017-09-26 18:36:36 | 2017-09-26 18:45:56 | 
| 143897 | APOEL Nicosia | Tottenham Hotspur | 2017-09-26 18:45:56 | 2017-09-26 18:47:34 | 
| 206117 | APOEL Nicosia | Tottenham Hotspur | 2017-09-26 18:47:34 | 2017-09-28 19:22:49 | 
| 112775 | AS Monaco  | Besiktas JK  | 2017-09-28 19:22:49 | 2017-09-28 19:37:41 | 
| 128744 | AS Monaco  | Besiktas JK  | 2017-09-28 19:37:41 | 2017-09-28 19:49:06 | 
| 238778 | AS Monaco  | Besiktas JK  | 2017-09-28 19:49:06 | 2017-09-28 19:54:15 | 
| 37271 | AS Monaco  | Besiktas JK  | 2017-09-28 19:54:15 | 2017-09-28 20:13:15 | 
| 81647 | AS Monaco  | Besiktas JK  | 2017-09-28 20:13:15 | 2017-09-28 20:17:44 | 
| 65930 | AS Monaco  | Besiktas JK  | 2017-09-28 20:17:44 | 2017-09-28 20:20:31 | 
| 45845 | AS Monaco  | Besiktas JK  | 2017-09-28 20:20:31 | 2017-09-28 20:21:50 | 
| 110165 | AS Monaco  | Besiktas JK  | 2017-09-28 20:21:50 | 2017-09-28 20:35:16 | 
| 4856 | AS Monaco  | Besiktas JK  | 2017-09-28 20:35:16 | 2017-09-28 20:40:36 | 
| 148769 | AS Monaco  | Besiktas JK  | 2017-09-28 20:40:36 | 2017-09-28 20:54:01 | 
| 34760 | AS Monaco  | Besiktas JK  | 2017-09-28 20:54:01 | 2017-09-28 21:02:34 | 
| 182633 | AS Monaco  | Besiktas JK  | 2017-09-28 21:02:34 | 2017-09-28 21:13:20 | 
| 230996 | AS Monaco  | Besiktas JK  | 2017-09-28 21:13:20 | 2017-09-28 21:20:41 | 
| 66761 | AS Monaco  | Besiktas JK  | 2017-09-28 21:20:41 | 2017-09-28 21:25:49 | 
| 243059 | AS Monaco  | Besiktas JK  | 2017-09-28 21:25:49 | 2017-09-28 21:43:19 | 
+--------+---------------+-------------------+---------------------+---------------------+ 

所以我希望在球隊發生變化時不要從前一隊獲得價值。因此指數206117,希臘人競技的最後一行'x熱刺將有空列next_dtScraped

+1

請提供一些示例數據。 –

回答

0
import numpy as np 
x["next_dtScraped"] = np.where(x["away_team"] == x["away_team"].shift(-1),x["dtScraped"].shift(-1),None) 
+0

這一個完美的工作,我只需要添加到datetime的轉換。使用的代碼: 'code'x [「next_dtScraped」] = np.where(x [「away_team」] == x [「away_team」]。shift(-1),x [「dtScraped」]。shift(-1 ),無) x [「next_dtScraped」] = pd.to_datetime(x [「next_dtScraped」]) –

+0

@IvoFilipe很高興它有助於美好的一天 – Wen

0

可以使用減少:

mask = x["away_team"] == x["away_team"].shift(-1) 
mask = reduce(lambda x,y: x and y, mask) 
if mask: 
    x["dtScraped"].shift(-1) 
else: 
    None 

x["away_team"] == x["away_team"].shift(-1)爲您提供了布爾的列表,以便使用減少,你可以看到,如果它是非常平等。

對於你的錯誤,我不知道你試圖做什麼,但你應該發佈你的錯誤,所以我們可以知道如何提供幫助。

編輯:想我已經得到了你有什麼語法錯誤,試試這個:

x["next_dtScraped"] = x["dtScraped"].shift(-1) if (x["away_team"] == x["away_team"].shift(-1))  else  None 
0

這應做到:

df["next_dtScraped"] = df["next_dtScraped"].apply(lambda x: df["dtScraped"].shift(-1) if df["away_team"] == df["away_team"].shift(-1) else x) 

或者:

x["next_dtScraped"] = x.apply(lambda c: c["dtScraped"].shift(-1) if c["away_team"] == c["away_team"].shift(-1) else None) 

不知道你需要哪一個:)