我有几微秒,我想从 Pandas 列中截断它。我尝试了类似 analyze_me['how_long_it_took_to_order'] = analyze_me['how_long_it_took_to_order'].apply(lambda x: x.replace(microsecond=0) 的东西,但出现了 replace() takes no keyword arguments 错误。

例如:我希望 00:19:58.582052 变成 00:19:58 或 00:19:58.58

python - 如何从timedelta中删除微秒-LMLPHP

最佳答案

您的 how_long_it_took_to_order 列似乎是字符串 ( object ) dtype。

所以试试这个:

analyze_me['how_long_it_took_to_order'] = \
    analyze_me['how_long_it_took_to_order'].str.split('.').str[0]

或者:
analyze_me['how_long_it_took_to_order'] = \
    analyze_me['how_long_it_took_to_order'].str.replace('(\.\d{2})\d+', r'\1')

对于“厘秒”,例如:00:19:58.58

关于python - 如何从timedelta中删除微秒,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46204241/

10-11 08:41