我想將包含字符串日期的列轉換為其他類型的字符串日期。我嘗試像這樣使用 apply : self.df['mydatestring'] = self.df['mydatestring'].apply( lambda x: datetime.datetime.strptime(x, '%Y-%m-%d').strftime('%d/%m/%y') if pd.notnull(x) else '', )但我 ValueError : The truth value of a Series is ambiguous. 不明白為什么,你能幫忙嗎?
1 回答

郎朗坤
TA貢獻1921條經驗 獲得超9個贊
你為什么不使用pd.to_datetime:
self.df['mydatestring'] = (pd.to_datetime(self.df['mydatestring'],
format='%Y-%m-%d',
errors='coerce')
.dt.strftime('%d/%m/%y')
.fillna('')
)
添加回答
舉報
0/150
提交
取消