我有一个嵌套字典熊猫。它从(STRING, DATE)
的元组转换为包含特定列和值的字典。我一直试图找出获取单个单元格数据的语法。例如,我想调用[('SYY','1997-06-30')] ['dvt']并得到99.5740。我也尝试过以各种形式使用df.get和[(tuple)],没有任何运气。我收到的错误是
KeyError :('SYY','2016-06-30')
我印刷的熊猫看起来像这样:
ivncf dvt fincf chech
SYY 1997-06-30 -213.3560 99.5740 -274.8150 9.9370
1998-06-30 -335.5300 110.9280 -29.6420 -7.4080
1999-06-30 -261.7350 126.6910 -284.5530 39.0150
2000-06-30 -459.3920 145.4180 -239.5090 9.8250
2001-06-30 -338.7510 173.7010 -639.8580 -23.3850
2002-06-30 -630.3000 225.5300 -359.9840 94.6960
2003-06-30 -681.8250 273.8520 -550.5280 139.0080
2004-06-30 -683.8110 321.3530 -641.8510 -137.7410
2005-06-30 -413.4400 368.7920 -784.2700 -8.0280
2006-06-30 -608.6710 408.2640 -504.6170 10.2190
2007-06-30 -648.7110 456.4380 -748.2500 5.9750
2008-06-30 -555.5600 513.5930 -698.5780 343.6800
2009-06-30 -658.6630 557.4870 -379.6430 535.5320
2010-06-30 -656.3200 585.7340 -667.0300 -433.2080
2011-06-30 -679.5560 604.5000 -377.9070 54.3220
2012-06-30 -903.6290 628.0240 -442.6490 49.1020
2013-06-30 -911.8820 654.8710 -874.2080 -276.5820
2014-06-30 -576.8380 673.5680 -915.8580 0.7610
2015-06-30 -654.3460 705.5390 3897.5620 4716.9980
2016-06-30 -600.8280 695.4690 -2404.7310 -1210.7440
有什么建议吗?谢谢!
答案 0 :(得分:3)
我认为你需要DataFrame.loc
:
print (df.loc[('SYY', '1997-06-30'), 'dvt'])
99.574
对于复杂的选择,请使用slicers:
idx = pd.IndexSlice
print (df.loc[idx['SYY', '1997-06-30'], 'dvt'])
99.574
idx = pd.IndexSlice
print (df.loc[idx['SYY', '1997-06-30':'2001-06-30'], idx['dvt':'fincf']])
dvt fincf
SYY 1997-06-30 99.574 -274.815
1998-06-30 110.928 -29.642
1999-06-30 126.691 -284.553
2000-06-30 145.418 -239.509
2001-06-30 173.701 -639.858
idx = pd.IndexSlice
print (df.loc[idx['SYY', '1997-06-30'], idx['dvt':'chech']])
dvt 99.574
fincf -274.815
chech 9.937
Name: (SYY, 1997-06-30), dtype: float64
idx = pd.IndexSlice
print (df.loc[idx['SYY', '1997-06-30':'2003-06-30'], 'dvt'])
SYY 1997-06-30 99.574
1998-06-30 110.928
1999-06-30 126.691
2000-06-30 145.418
2001-06-30 173.701
2002-06-30 225.530
2003-06-30 273.852
Name: dvt, dtype: float64