2017-05-08 63 views
1

如果'狀態'在'4'中。封閉」 & 'closeDate' 是在 '2017年3月27日' 然後更改/替換/更新 '' 在日期值 'correctClosedDate' 列選擇具有一定條件的行,然後用另一列中的某些值替換那些行

enter image description here

 df= pd.DataFrame(
       {"ID":['A','B','C','D','E'], 
       "state":['3. Cancelled', '4. Closed', '4. Closed', '3. Cancelled', '4. Closed' ], 
       "closeDate":['2017/4/12','2017/3/27','2017/4/1','2017/4/29','2017/3/27'], 
       "correctCloseDate":['', '2017/1/5', '', '', '2017/2/27'] 
       }) 

這裏closeDate,我試過找到這些條件的行,我不知道如何用'correctCloseDate'中的值替換這些行。

df.loc[df['state'].isin('4. Closed') & df['closeDate'].isin(['2017-03-27']) == True] 

我有一個錯誤說:

類型錯誤:只有列表類似的對象可以被傳遞到ISIN(),你通過了[STR]

我想要的結果看起來像這個。

enter image description here

任何幫助將不勝感激!

回答

0

我想你需要to_datetime,也被(以大熊貓NaN的日期),添加的參數errors='coerce'的轉換不是日期時間到NaT

#if necessary convert to datetime 
df['closeDate'] = pd.to_datetime(df['closeDate']) 
df['correctCloseDate'] = pd.to_datetime(df['correctCloseDate'], errors='coerce') 

然後創建boolean maskSeries.mask通過面膜代替,最後刪除不必要的列通過drop

mask = (df['state'] == '4. Closed') & (df['closeDate'] == '2017-03-27') 
df['closeDate'] = df['closeDate'].mask(mask, df['correctCloseDate']) 
df = df.drop('correctCloseDate', axis=1) 
print (df) 
    ID closeDate   state 
0 A 2017-04-12 3. Cancelled 
1 B 2017-01-05  4. Closed 
2 C 2017-04-01  4. Closed 
3 D 2017-04-29 3. Cancelled 
4 E 2017-02-27  4. Closed 

替代在使用loc用於替換:

mask = (df['state'] == '4. Closed') & (df['closeDate'] == '2017-03-27') 
df.loc[mask, 'closeDate'] = df['correctCloseDate'] 
df = df.drop('correctCloseDate', axis=1) 
print (df) 
    ID closeDate   state 
0 A 2017-04-12 3. Cancelled 
1 B 2017-01-05  4. Closed 
2 C 2017-04-01  4. Closed 
3 D 2017-04-29 3. Cancelled 
4 E 2017-02-27  4. Closed 

解決方案,只有字符串 - 但後來改變2017-03-272017/3/27

mask = (df['state'] == '4. Closed') & (df['closeDate'] == '2017/3/27') 
df['closeDate'] = df['closeDate'].mask(mask, df['correctCloseDate']) 
df = df.drop('correctCloseDate', axis=1) 
print (df) 
    ID closeDate   state 
0 A 2017/4/12 3. Cancelled 
1 B 2017/1/5  4. Closed 
2 C 2017/4/1  4. Closed 
3 D 2017/4/29 3. Cancelled 
4 E 2017/2/27  4. Closed 
+0

感謝全面的答案,並顯示不同的選擇! – Johnny

0
# you can use numpy.where to locate the rows you need and get the correct date for each row based on your condition and put them back in a new column(or back to correctCloseDate if you want) 

df['final_correctCloseDate'] = np.where((df['state'] == '4. Closed') & (df['closeDate'] == '2017-03-27'), df.correctCloseDate, df.closeDate) 
+0

向下投票者:介意解釋? – Allen

+0

這也適用於艾倫。看到有很多不同的方式來解決問題總是讓我感到驚訝。非常感謝!! – Johnny

相關問題