猿问

在字符串上应用 Lambda:ValueError:系列的真值不明确

我想将包含字符串日期的列转换为其他类型的字符串日期。我尝试像这样使用 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. 不明白为什么,你能帮忙吗?


富国沪深
浏览 123回答 1
1回答

郎朗坤

你为什么不使用pd.to_datetime:self.df['mydatestring'] = (pd.to_datetime(self.df['mydatestring'],                                           format='%Y-%m-%d',                                           errors='coerce')                             .dt.strftime('%d/%m/%y')                             .fillna('')                          )
随时随地看视频慕课网APP

相关分类

Python
我要回答