【问题标题】:20 days prior to 1st of October, 2018 ("Pandas")2018 年 10 月 1 日之前的 20 天(“熊猫”)
【发布时间】:2020-03-20 01:04:00
【问题描述】:

有一个案例从 2 天开始就解决了以下问题:

end_Date = "1st October, 2018"
end_Date = pd.to_datetime(end_Date)
start_Date = end_Date - pd.Timedelta(days = 20)

df360[(df360.claim_date <= str(end_Date)) & (df360.claim_date >= str(start_Date))['claim_amount'].sum()

当我这样做时: 我收到以下错误

文件“”,第 1 行 df360[(df360.claim_date = str(start_Date))['claim_amount'].sum() ^ SyntaxError: 解析时出现意外的 EOF

【问题讨论】:

  • 把它拆分成更小的部分——变量——看看你有没有忘记()或[]
  • 现在我看到您在df360[ 中打开了[,但您没有为此关闭]。

标签: python pandas datetime pandoc eof


【解决方案1】:

使用.loc 修复您的代码

df360.claim_date = pd.to_datetime(df360.claim_date)
df360.loc[(df360.claim_date <= end_Date) & (df360.claim_date >= start_Date),'claim_amount'].sum()

【讨论】:

  • 我明白了!非常感谢大家。
【解决方案2】:

我在 jupyter notebook 中做过:

end_Date = "1st October, 2018"
end_Date = pd.to_datetime(end_Date)
start_Date = end_Date - pd.Timedelta(days = 20)

然后:

df360['claim_amount'].dtypes

看看你是否得到这个作为输出:

dtype('float64')

而不是:

dtype('o')

然后粘贴这段代码:

df360[(df360.claim_date <= end_Date) & (df360.claim_date >= start_Date)]['claim_amount'].sum()

我成功获得了输出!并感谢您的帮助!

【讨论】:

    猜你喜欢
    • 2019-09-08
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-09
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    相关资源
    最近更新 更多