鱼C论坛

 找回密码
 立即注册
查看: 9720|回复: 5

[已解决]pandas问题,已经晕了

[复制链接]
发表于 2018-9-12 19:33:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
问题:
运行finish_time = dt["信息归档完成时间"][i] - dt["业务受理接受时间"][i]出现错误,而运行finish_time = dt["信息归档完成时间"][i] - dt["业务受理完成时间"][i]没有问题,我都不知道怎么办?
代码:
  1. import pandas as pd
  2. # 打开合并后数据表
  3. df = pd.read_excel("01.xlsx")
  4. # 筛选出信息归档时间不为0的数据
  5. dt = df.loc[df["信息归档完成时间"] != 0]
  6. list_time = []
  7. for i in range(dt.shape[0]):
  8.     finish_time = dt["信息归档完成时间"][i] - dt["业务受理接受时间"][i]
  9.     list_time.append(finish_time)
  10. '''
  11. n = 0
  12. table = []
  13. sum_table = 0
  14. for each in list_time:
  15.     [a,b] = str(each).split("days",1)
  16.     table.append(a)
  17.     sum_table += int(a)
  18.     n += 1
  19. [a,b]=str(list_time[0])
  20. '''
  21. print(list_time)
复制代码

错误原因:

Traceback (most recent call last):
  File "C:\Users\Chysial\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\indexes\base.py", line 3078, in get_loc
    return self._engine.get_loc(key)
  File "pandas\_libs\index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: '业务受理接受时间'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Chysial\Desktop\efficiency_analysis.py", line 8, in <module>
    finish_time = dt["信息归档完成时间"][i] - dt["业务受理接受时间"][i]
  File "C:\Users\Chysial\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\frame.py", line 2688, in __getitem__
    return self._getitem_column(key)
  File "C:\Users\Chysial\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\frame.py", line 2695, in _getitem_column
    return self._get_item_cache(key)
  File "C:\Users\Chysial\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\generic.py", line 2489, in _get_item_cache
    values = self._data.get(item)
  File "C:\Users\Chysial\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\internals.py", line 4115, in get
    loc = self.items.get_loc(item)
  File "C:\Users\Chysial\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\indexes\base.py", line 3080, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\_libs\index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: '业务受理接受时间'


如果运行完成时间是对的,得出结果是:
[Timedelta('119 days 23:24:57'), NaT, Timedelta('134 days 22:58:20'), Timedelta('97 days 00:34:39'), NaT, NaT, Timedelta('99 days 03:51:44'), NaT]
谁能帮我解决一下什么问题啊,附件是excel,无法上传,这是

业务受理接收时间        业务受理完成时间        信息归档完成时间        受理时间
2017/11/21 8:43        2017/11/21 9:08        2018/3/21 8:33        2017/11/21 8:43
2017/12/4 14:59        2017/12/4 15:02                2017/12/4 14:59
2017/11/6 10:07        2017/11/6 10:30        2018/3/21 9:29        2017/11/6 10:07
2017/12/14 8:40        2017/12/14 9:23        2018/3/21 9:58        2017/12/14 8:40
2018/1/18 13:14        2018/1/18 13:19                2018/1/18 13:14
2017/10/25 9:45        2017/10/25 9:50                2017/10/25 9:45
2017/12/13 8:58        2017/12/13 9:12        2018/3/22 13:04        2017/12/13 8:58
2017/12/4 14:49        2017/12/4 14:54                2017/12/4 14:49
最佳答案
2018-9-12 21:20:11
Chysial 发表于 2018-9-12 21:14
>>> df1=df.loc["信息归档完成时间"]
Traceback (most recent call last):
  File "C:%users\Chysial\A ...

方向应该对了吧?

KeyError: 'the label [信息归档完成时间] is not in the [index]'
没有这一栏,看回 excel 里面有什么东西
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-9-12 19:38:30 | 显示全部楼层
收受,Keyerror,虽然我没用过pandas
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-12 19:49:08 | 显示全部楼层
没有用过
第 5 就有问题了吧?
dt1 = df.loc["信息归档完成时间"]
dt2 = df.loc["业务受理接受时间"]

for ... (dt1[i] - dt2[i])
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-9-12 21:14:53 | 显示全部楼层
claws0n 发表于 2018-9-12 19:49
没有用过
第 5 就有问题了吧?
dt1 = df.loc["信息归档完成时间"]

>>> df1=df.loc["信息归档完成时间"]
Traceback (most recent call last):
  File "C:\Users\Chysial\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\indexing.py", line 1790, in _validate_key
    error()
  File "C:\Users\Chysial\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [信息归档完成时间] is not in the [index]'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    df1=df.loc["信息归档完成时间"]
  File "C:\Users\Chysial\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\indexing.py", line 1478, in __getitem__
    return self._getitem_axis(maybe_callable, axis=axis)
  File "C:\Users\Chysial\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\indexing.py", line 1911, in _getitem_axis
    self._validate_key(key, axis)
  File "C:\Users\Chysial\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\indexing.py", line 1798, in _validate_key
    error()
  File "C:\Users\Chysial\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\indexing.py", line 1785, in error
    axis=self.obj._get_axis_name(axis)))
KeyError: 'the label [信息归档完成时间] is not in the [index]'
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-12 21:20:11 | 显示全部楼层    本楼为最佳答案   
Chysial 发表于 2018-9-12 21:14
>>> df1=df.loc["信息归档完成时间"]
Traceback (most recent call last):
  File "C:%users\Chysial\A ...

方向应该对了吧?

KeyError: 'the label [信息归档完成时间] is not in the [index]'
没有这一栏,看回 excel 里面有什么东西
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-9-13 09:10:20 | 显示全部楼层
claws0n 发表于 2018-9-12 21:20
方向应该对了吧?

KeyError: 'the label [信息归档完成时间] is not in the '

我知道什么问题了,loc所有的pandas都是返回行的数据,不能直接用列来,想返回列可以直接df["列标签"]就行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-1 13:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表