gpt4 book ai didi

python - 如果该列大于所述日期,如何绕过此错误以使该列为零? "TypeError: invalid type promotion "

转载 作者:行者123 更新时间:2023-12-01 06:28:30 28 4
gpt4 key购买 nike

df_main['month_1']= np.where(df_main['month_1'] >='2020-02-01',0,df_main['month_1'])

如果日期是 2020 年 2 月 1 日,我需要 Month_1 列中的所有项目均为零。我也尝试了“02/01/2020”格式,但不起作用。

最佳答案

由于您的列数据类型是时间戳,因此您无法使用 str '2020-02-01' 与您的列进行比较,因此您还需要一个时间戳值:pd.Timestamp(2020, 2,1)

您可以使用pandas.Series.map :

df_main['month_1'] = df_main['month_1'].map(lambda x: 0 if x >= pd.Timestamp(2020, 2,1) else x)

或者您可以过滤和分配:

df_main['month_1'][ df_main['month_1'] >= pd.Timestamp(2020, 2,1)] = 0 

关于python - 如果该列大于所述日期,如何绕过此错误以使该列为零? "TypeError: invalid type promotion ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60017006/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com