嵌套字典Python中不存在键

时间:2020-09-30 18:33:06

标签: python excel pandas dataframe

我有一本字典,其中使用以下代码制作了嵌套字典:

CallData = pd.read_excel(
    FilePath + 'Call Center Work Rules.xlsx',
    sheet_name='Data Agg.')

call_volume = CallData.set_index('Date').to_dict(orient='index')

CallData只是使用Pandas读取Excel工作表并从中获取一系列数据。然后,我将其分配给call_volume。

如果我打印(call_volume),我得到以下结果:

for k, v in call_volume.items():
    print(k, v)

2015-01-31 00:00:00 {'Calls': 82034.0, 'AVG Call Time': 6.471023816756425, 'Total Call Time': 530843.9677837966, 'Orders': 1633581.4722911494}
2015-02-28 00:00:00 {'Calls': 78007.5, 'AVG Call Time': 6.743145573105226, 'Total Call Time': 526015.9282940059, 'Orders': 653432.5889164599}
2015-03-31 00:00:00 {'Calls': 84425.5, 'AVG Call Time': 6.608714001241683, 'Total Call Time': 557943.9839118298, 'Orders': 980148.8833746896}
2015-04-30 00:00:00 {'Calls': 71089.0, 'AVG Call Time': 6.959075144942898, 'Total Call Time': 494713.6929788457, 'Orders': 847808.105872622}
2015-05-31 00:00:00 {'Calls': 77414.0, 'AVG Call Time': 6.732396178666107, 'Total Call Time': 521181.717775258, 'Orders': 1186931.3482216706}
2015-06-30 00:00:00 {'Calls': 86811.0, 'AVG Call Time': 6.999769374961217, 'Total Call Time': 607656.9792097582, 'Orders': 1356492.9693961951}
2015-07-31 00:00:00 {'Calls': 81995.0, 'AVG Call Time': 7.051136987681479, 'Total Call Time': 578157.9773049429, 'Orders': 1861042.1836228287}
2015-08-31 00:00:00 {'Calls': 62538.0, 'AVG Call Time': 7.995745553136054, 'Total Call Time': 500037.9354020225, 'Orders': 1116625.310173697}
2015-09-30 00:00:00 {'Calls': 45677.5, 'AVG Call Time': 7.885181506929048, 'Total Call Time': 360175.3782827516, 'Orders': 744416.8734491315}
2015-10-31 00:00:00 {'Calls': 52542.0, 'AVG Call Time': 8.083627399752345, 'Total Call Time': 424729.9508377877, 'Orders': 1000827.1298593879}
2015-11-30 00:00:00 {'Calls': 51352.0, 'AVG Call Time': 7.6086368082338165, 'Total Call Time': 390718.71737642295, 'Orders': 1751447.4772539283}
2015-12-31 00:00:00 {'Calls': 58871.0, 'AVG Call Time': 7.675463025450389, 'Total Call Time': 451862.1837712898, 'Orders': 2251861.0421836227}
2016-01-31 00:00:00 {'Calls': 68664.0, 'AVG Call Time': 7.7456291451070935, 'Total Call Time': 531845.8796196334, 'Orders': 1712810.1736972705}

我以这种方式进行设置,以便可以参考日期和诸如“通话”之类的特定值。

但是,当我尝试引用密钥时,出现以下错误:

print(call_volume['2015-01-31 00:00:00'])

错误:

Traceback (most recent call last):
  File "D:/Personal Files/Technical Development/PycharmProjects/Call Center Headcount Model/Call Center Headcount Model.py", line 55, in <module>
    print(call_volume['2015-01-31 00:00:00'])
KeyError: '2015-01-31 00:00:00'                     

我对字典的引用不正确吗?

编辑:要转换为字符串的代码:

 def convert(call_volume):
        if isinstance(call_volume, str):
            return str(call_volume)
        elif isinstance(call_volume, collections.Mapping):
            return dict(map(convert, call_volume.iteritems()))
        elif isinstance(call_volume, collections.Iterable):
            return type(call_volume)(map(convert, call_volume))
        else:
            return call_volume

错误:

D:/Personal Files/Technical Development/PycharmProjects/Call Center Headcount Model/Call Center Headcount Model.py:57: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.9 it will stop working
  elif isinstance(call_volume, collections.Mapping):
Traceback (most recent call last):
  File "D:/Personal Files/Technical Development/PycharmProjects/Call Center Headcount Model/Call Center Headcount Model.py", line 65, in <module>
    print(convert(call_volume))
  File "D:/Personal Files/Technical Development/PycharmProjects/Call Center Headcount Model/Call Center Headcount Model.py", line 58, in convert
    return dict(map(convert, call_volume.iteritems()))
AttributeError: 'dict' object has no attribute 'iteritems'

1 个答案:

答案 0 :(得分:0)

这是引用时间戳的代码:

print(call_volume[pd.Timestamp('2015-01-31 00:00:00')])