Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中使用.pivot时出现键错误_Python_Pandas_Pivot - Fatal编程技术网

在python中使用.pivot时出现键错误

在python中使用.pivot时出现键错误,python,pandas,pivot,Python,Pandas,Pivot,我研究了很多与透视表相关的问题,但没有发现一个解决了这个特定问题。我有这样一个数据框: Drug Timepoint Tumor Volume (mm3) Capomulin 0 45.000000 5 44.266086 10 43.084291 15 42.064317 20

我研究了很多与透视表相关的问题,但没有发现一个解决了这个特定问题。我有这样一个数据框:

Drug        Timepoint    Tumor Volume (mm3)
Capomulin   0             45.000000
            5             44.266086
            10            43.084291
            15            42.064317
            20            40.716325
... ... ...
Zoniferol   25            55.432935
            30            57.713531
            35            60.089372
            40            62.916692
            45            65.960888
我试图将数据转换为数据轴,以便药物名称成为列标题,时间点成为新索引,肿瘤体积是值。我在网上查到的所有信息都告诉我要使用:

mean_tumor_volume_gp.pivot(index = "Timepoint",
                           columns = "Drug",
                           values = "Tumor Volume (mm3)")
但是,当我运行此单元格时,会收到错误消息:

KeyError                                  Traceback (most recent call last)
<ipython-input-15-788b92ba981e> in <module>
      2 mean_tumor_volume_gp.pivot(index = "Timepoint",
      3                             columns = "Drug",
----> 4                             values = "Tumor Volume (mm3)")
      5 

KeyError: 'Timepoint'
keyrerror回溯(最近一次调用)
在里面
2平均肿瘤体积总支点(index=“Timepoint”,
3列=“药物”,
---->4个值=“肿瘤体积(mm3)”)
5.
KeyError:“时间点”

这怎么会是一个关键错误?键“Timepoint”是原始DF中的一列。

您缺少函数参数之间的逗号。在逗号中输入的只是一个键错误:“Timepoint”@jason-hernandez-73您的pivot代码看起来完全合法。我认为您应该重新设置初始数据帧的索引。所以你将药物作为索引。你缺少函数参数之间的逗号。在逗号中插入只会给我一个关键错误:“Timepoint”@jason-hernandez-73你的pivot代码看起来完全合法。我认为您应该重新设置初始数据帧的索引。所以你将药物作为索引。