我需要改變我的代碼以NumPy
二維數組,而不是pandas
數據幀的工作:更改代碼來處理NumPy的陣列,而不是熊貓據幀
df = pd.DataFrame(data=np.array([[nan, 2, 3], [4, 5, 6], [7, 8, 9]]), columns=["col1", "col2", "col3"])
list_of_NA_features = ["col1"]
for feature in list_of_NA_features:
for index,row in df.iterrows():
if (pd.isnull(row[feature]) == True):
missing_value = 5 # for simplicity, let's put 5 instead of a function
df.ix[index,feature] = missing_val
什麼是來爲他們做for index,row in df.iterrows():
,pd.isnull(row[feature]) == True
和的正確方法NumPy
數組?
這是我迄今所做的:
np_arr = df.as_matrix
for feature in list_of_NA_features:
for feature in xrange(np_arr.shape[1]):
# ???
我怎樣才能到能夠執行np_arr[irow,feature]
行的指數?在NumPy
數組中,將值分配給特定行和列的正確方法是什麼:?
UPDATE
我通過刪除功能fill_missing_values
並與值5
代它簡化了代碼。但是,在我的真實情況下,我需要估計缺失值。
我認爲正確的方法是使用量化的方法。但是如果不能看到一個小的可重複的樣本數據集和一個期望的數據集就很困難...... ;-) – MaxU
我建議添加一個簡單的示例數據框。 –
@AndrasDeak:這只是一個返回整數的函數。事實上,在這種情況下它並不重要。所以我沒有解釋這個功能。 – Dinosaurius