KeyError:尝试绘制多个直方图时为0

时间:2020-03-20 22:01:26

标签: python pandas matplotlib

在绘制多个直方图时遇到麻烦。我收到错误消息:

pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12368)()

pandas\hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12322)()

KeyError: 0

这是我写的代码:

xaxes = ['price','bedrooms','sqft_living','sqft_lot','floors','waterfront',
        'view','condition','grade','sqft_above','sqft_basement','yr_built',
        'yr_renovated','zipcode','lat','long','sqft_living15','sqft_loft15']
a,b = plt.subplots(4,5)
b = b.ravel()
for idx,ax in enumerate(b):
    ax.hist(file[idx])
    ax.set_title(titles[idx])
    ax.set_xlabel(xaxes[id])
plt.tight_layout()

以下是我的数据示例:

{'bathrooms': {0: 1.0,
  1: 2.25,
  2: 1.0,
  3: 3.0,
  4: 2.0,
  5: 4.5,
  6: 2.25,
  7: 1.5,
  8: 1.0,
  9: 2.5},
 'bedrooms': {0: 3, 1: 3, 2: 2, 3: 4, 4: 3, 5: 4, 6: 3, 7: 3, 8: 3, 9: 3},
 'condition': {0: 3, 1: 3, 2: 3, 3: 5, 4: 3, 5: 3, 6: 3, 7: 3, 8: 3, 9: 3},
 'date': {0: '20141013T000000',
  1: '20141209T000000',
  2: '20150225T000000',
  3: '20141209T000000',
  4: '20150218T000000',
  5: '20140512T000000',
  6: '20140627T000000',
  7: '20150115T000000',
  8: '20150415T000000',
  9: '20150312T000000'},
 'floors': {0: 1.0,
  1: 2.0,
  2: 1.0,
  3: 1.0,
  4: 1.0,
  5: 1.0,
  6: 2.0,
  7: 1.0,
  8: 1.0,
  9: 2.0},
 'grade': {0: 7, 1: 7, 2: 6, 3: 7, 4: 8, 5: 11, 6: 7, 7: 7, 8: 7, 9: 7},
 'id': {0: 7129300520,
  1: 6414100192,
  2: 5631500400,
  3: 2487200875,
  4: 1954400510,
  5: 7237550310,
  6: 1321400060,
  7: 2008000270,
  8: 2414600126,
  9: 3793500160},
 'lat': {0: 47.511200000000002,
  1: 47.721000000000004,
  2: 47.737900000000003,
  3: 47.520800000000001,
  4: 47.616799999999998,
  5: 47.656100000000002,
  6: 47.309699999999999,
  7: 47.409500000000001,
  8: 47.512300000000003,
  9: 47.368400000000001},
 'long': {0: -122.25700000000001,
  1: -122.319,
  2: -122.23299999999999,
  3: -122.39299999999999,
  4: -122.045,
  5: -122.005,
  6: -122.32700000000001,
  7: -122.315,
  8: -122.337,
  9: -122.03100000000001},
 'price': {0: 221900.0,
  1: 538000.0,
  2: 180000.0,
  3: 604000.0,
  4: 510000.0,
  5: 1230000.0,
  6: 257500.0,
  7: 291850.0,
  8: 229500.0,
  9: 323000.0},
 'sqft_above': {0: 1180,
  1: 2170,
  2: 770,
  3: 1050,
  4: 1680,
  5: 3890,
  6: 1715,
  7: 1060,
  8: 1050,
  9: 1890},
 'sqft_basement': {0: 0,
  1: 400,
  2: 0,
  3: 910,
  4: 0,
  5: 1530,
  6: 0,
  7: 0,
  8: 730,
  9: 0},
 'sqft_living': {0: 1180,
  1: 2570,
  2: 770,
  3: 1960,
  4: 1680,
  5: 5420,
  6: 1715,
  7: 1060,
  8: 1780,
  9: 1890},
 'sqft_living15': {0: 1340,
  1: 1690,
  2: 2720,
  3: 1360,
  4: 1800,
  5: 4760,
  6: 2238,
  7: 1650,
  8: 1780,
  9: 2390},
 'sqft_lot': {0: 5650,
  1: 7242,
  2: 10000,
  3: 5000,
  4: 8080,
  5: 101930,
  6: 6819,
  7: 9711,
  8: 7470,
  9: 6560},
 'sqft_lot15': {0: 5650,
  1: 7639,
  2: 8062,
  3: 5000,
  4: 7503,
  5: 101930,
  6: 6819,
  7: 9711,
  8: 8113,
  9: 7570},
 'view': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0},
 'waterfront': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0},
 'yr_built': {0: 1955,
  1: 1951,
  2: 1933,
  3: 1965,
  4: 1987,
  5: 2001,
  6: 1995,
  7: 1963,
  8: 1960,
  9: 2003},
 'yr_renovated': {0: 0,
  1: 1991,
  2: 0,
  3: 0,
  4: 0,
  5: 0,
  6: 0,
  7: 0,
  8: 0,
  9: 0},
 'zipcode': {0: 98178,
  1: 98125,
  2: 98028,
  3: 98136,
  4: 98074,
  5: 98053,
  6: 98003,
  7: 98198,
  8: 98146,
  9: 98038}}

1 个答案:

答案 0 :(得分:0)

如果我以正确的方式理解您,并且变量 file 将您的数据包含在pandas数据框中,那么您将面临索引该数据框的问题。

file [idx]对应于file.loc [idx],这意味着“在我的数据框中给我一个并带有 idx 号”,而您需要的是列而不是一排。只需将其替换为file.loc [:,idx]。

检查this链接以获取有关索引和选择大熊猫的模式的详细信息。