[英]numpy.histogram: problems with empty cells in excel
我是使用python的新手,所以我不知道所有技術術語是否正確。
我正在使用xlrd從excel工作表中讀取數據,然后使用過濾器函數對其進行過濾,然后使用numpy.histogram函數創建直方圖。 現在我在excel工作表中有一個空單元格,並且numpy.histogram返回錯誤的結果:
這是我的代碼:
import xlrd
import openpyxl
import numpy as n
from numpy import *
file_location = "C:/Users/test.xlsx"
sheet_index = 2
range_hist = 23
lifetime_data = 3
low_salesyear = 1990
upp_salesyear = 2005
col_filter1 = 14
filter_value1 = 1
col_filter2 = 18
filter_value2 = 5
# open excel-file
workbook = xlrd.open_workbook(file_location)
# get sheet, index always start at 0
sheet = workbook.sheet_by_index(sheet_index)
#read all data in the sheet
list_device = [[sheet.cell_value(r,c) for c in range (sheet.ncols)] for r in range (1,sheet.nrows)]
# filter list for independent variables
listnew = list(filter(lambda x: x[col_filter1]==filter_value1 and x[col_filter2]==filter_value2 and low_salesyear <= x[0] <= upp_salesyear, list_device))
# low_salesyear <= x[0] <= upp_salesyear and
# select relevant data from filtered list for histogram and store it in list for histogram
list_for_hist = []
for i in range(len(listnew)):
list_for_hist.append(listnew[i][lifetime_data])
print (list_for_hist)
# create array from list
array_for_hist = array(list_for_hist)
# create histogram
hist = np.histogram(array_for_hist, bins = range(0,int(range_hist)))
print (hist)
我將所有變量放在開頭,以便可以輕松更改它們。 我敢肯定會有一種更優雅的方式來編寫整個程序。
我從excel篩選的列表如下所示:
[8.0, 19.0, 4.0, 4.0, 8.0, 3.0, 13.0, '', 10.0, 7.0, 17.0, 16.0, 8.0,
6.0, 13.0, 8.0, 7.0, 11.0, 12.0, 13.0, 4.0, 6.0, 5.0, 19.0, 8.0, 6.0]
從numpy.histogram生成的歷史記錄看起來像這樣:
(array([ 0, 10, 0, 1, 3, 1, 3, 2, 5, -25, 1, 1, 1,
3, 0, 0, 1, 1, 0, 2, 0, 0]), array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22]))
因此,我不明白為什么它會為bin 1返回10,為bin 9給出-25。如果我在excel中消除了空白單元格,則直方圖將正確。
有沒有辦法告訴我的程序忽略空單元格?
非常感謝你的幫助!
np.array(list_for_hist)
將np.array(list_for_hist)
所有項目轉換為通用list_for_hist
。 當list_for_hist
包含浮點數和字符串時, np.array
返回包含所有字符串的數組:
In [32]: np.array(list_for_hist)
Out[32]:
array(['8.0', '19.0', '4.0', '4.0', '8.0', '3.0', '13.0', '', '10.0',
'7.0', '17.0', '16.0', '8.0', '6.0', '13.0', '8.0', '7.0', '11.0',
'12.0', '13.0', '4.0', '6.0', '5.0', '19.0', '8.0', '6.0'],
dtype='|S32') <-- `|S32` means 32-byte strings.
因此,使用bins=range(0,int(23))
字符串進行裝箱可能會引發異常,但np.histogram
返回垃圾。
您需要將list_for_hist
轉換為僅包含浮點數的數組或列表:
import numpy as np
list_for_hist = [8.0, 19.0, 4.0, 4.0, 8.0, 3.0, 13.0, '', 10.0, 7.0, 17.0, 16.0,
8.0, 6.0, 13.0, 8.0, 7.0, 11.0, 12.0, 13.0, 4.0, 6.0, 5.0,
19.0, 8.0, 6.0]
array_for_hist = np.array(
[item if isinstance(item,(float,int)) else np.nan for item in list_for_hist])
# create histogram
hist, bin_edges = np.histogram(array_for_hist, bins=range(0,int(23)))
print (hist)
產量
[0 0 0 1 3 1 3 2 5 0 1 1 1 3 0 0 1 1 0 2 0 0]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.