从另一列中减去DateTimeIndex的1列将抛出ValueErrors和TypeErrors

2024-09-29 22:33:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个来自pandas的pivot_表,它有70列25行,我用

dosage = test_df[test_df > 0][[7,8]].dropna()

哪一个产生了表格

date    2014-02-19 14:15:05 2014-03-25 13:15:26
barcode     
V0HX8          0.5                   0.4
V0NLH          0.4                   0.3

我正在尝试创建一个列“difference”,它看起来像:

date    2014-02-19 14:15:05 2014-03-25 13:15:26 Diff
barcode     
V0HX8          0.5                   0.4        -0.1
V0NLH          0.4                   0.3        -0.1

使用以下代码:

dosage["Diff"] = dosage["2014-03-25 13:15:26"] - dosage["2014-02-19 14:15:05"]

但是得到错误:

ValueError: Wrong number of items passed 2, placement implies 1
TypeError: an integer is required

我还尝试使用以下代码:

dosage["Diff"] = dosage[[1]] - dosage[[0]] but get the error: KeyError: 'Diff'

Tags: 代码testpandasdfdate错误diffbarcode

热门问题