請幫忙!我遇到了一個問題,當按第二列和第三列按降序對 numpy 陣列進行排序時,因此無法執行此操作。一切正常,直到代碼遇到大于 9 的數字。你如何解決這個問題? 在此處輸入圖片說明
import numpy as np
data = [
['Other Theft', 2003, 5, 12, 16, 15, 'Strathcona', 49.269802, -123.083763],
['Other Theft', 2003, 5, 7, 15, 20, 'Strathcona', 49.269802, -123.083763],
['Other Theft', 2003, 4, 23, 16, 40, 'Strathcona', 49.269802, -123.083763],
['Other Theft', 2003, 4, 20, 11, 15, 'Strathcona', 49.269802, -123.083763],
['Other Theft', 2003, 4, 12, 17, 45, 'Strathcona', 49.269802, -123.083763],
['Other Theft', 2003, 3, 26, 20, 45, 'Strathcona', 49.269802, -123.083763],
['Offence Against a Person', 2015, 8, 11,'unknown', 'unknown', 'unknown', 0.000000, 0.000000],
['Break and Enter Residential/Other', 2003, 3, 10, 12, 0, 'Kerrisdale', 49.228051, -123.146610],
['Mischief', 2003, 6, 28, 4, 13, 'Dunbar-Southlands', 49.255559, -123.193725],
['Mischief', 2017, 3, 26, 23, 0, 'Sunset', 49.21431483, -123.101945],
['Other Theft', 2003, 2, 16, 9, 2, 'Strathcona', 49.269802, -123.083763],
['Break and Enter Residential/Other', 2003, 7, 9, 18, 15, 'Grandview-Woodland', 49.267734, -123.067654],
['Other Theft', 2003, 1, 31, 19, 45, 'Strathcona', 49.269802, -123.083763],
['Mischief', 2003, 9, 27, 1, 0, 'Dunbar-Southlands', 49.253762, -123.194407],
['Offence Against a Person', 2017, 1 , 24, 'unknown', 'unknown', 'unknown', 0.000000, 0.000000],
['Break and Enter Residential/Other', 2003, 4, 19, 18, 0, 'Grandview-Woodland', 49.267814, -123.067441],
['Break and Enter Residential/Other', 2003, 9, 24, 18, 30, 'Grandview-Woodland', 49.267731, -123.067302],
['Break and Enter Residential/Other', 2003, 11, 5, 8, 12, 'Sunset', 49.226430, -123.085283],
['Break and Enter Commercial', 2003, 9, 26, 2, 30, 'West End', 49.284715, -123.122824],
['Break and Enter Residential/Other', 2003, 10, 21, 10, 0, 'Grandview-Woodland', 49.267811, -123.067089],
['Other Theft', 2003, 1, 25, 12, 30, 'Strathcona', 49.269802, -123.083763],
['Offence Against a Person', 2003, 2, 12, 'unknown', 'unknown', 'unknown', 0.000000, 0.000000],
['Other Theft', 2003, 1, 9, 6, 45, 'Strathcona', 49.269802, -123.083763],
['Offence Against a Person', 2008, 2, 6, 'unknown', 'unknown', 'unknown', 0.000000, 0.000000],
]
np_array = np.array(data)
bool_column = np.int_(np.empty(np_array.shape[0]))
for line in range(np_array.shape[0]):
if np_array[line,4] and np_array[line,5] == 'unknown':
bool_column[line] = int(0)
else:
bool_column[line] = 1
np_array = np.append(np_array, np.reshape(bool_column,(np_array.shape[0],-1)), axis=1) #add bool col
#sort
np_array = np_array[np_array[:,2].argsort()]#month sort
np_array = np_array[np_array[:,1].argsort(kind='stable')[::-1]] #year sort with month
#count spaces foe nice data output
equals_spaces = []
temp_for_cicle = 0
for num_in_line in range(np_array.shape[1]):
for line in range (np_array.shape[0]):
if len(np_array[line][num_in_line]) > temp_for_cicle:
temp_for_cicle = len(np_array[line][num_in_line])
equals_spaces.append(temp_for_cicle)
temp_for_cicle = 0
#data output
for line in range(np_array.shape[0]):
for num_in_line in range (np_array.shape[1]):
print('{:<{}}'.format(np_array[line][num_in_line],equals_spaces[num_in_line]), end='')
if num_in_line 1 == np_array.shape[1]:
print("")
else:
print(" | ", end='')
uj5u.com熱心網友回復:
如果您在轉換為 numpy 之前進行排序并跳過numpy.argsort呼叫,它會起作用:
data.sort( key=lambda row: (-row[1], -row[2]) )
np_array = np.array(data)
使用的建議pandas可能是最好的建議。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/348719.html
上一篇:我正在嘗試實作:np.maximum.outerinPython3x但我收到此錯誤:NotImplementedError
