我有一些數據(42特徵)在幾個月內收集的人(最大-6; 因不同條目而異),每個月的價值是自己的行:構建一個從每個索引值的多個附加
有9267倍唯一ID的值(設定爲索引)和多達50 000行的DF。 我想將其轉換爲42個* 6的特徵向量每個ID(即使有些會有很多的NaN存在的),這樣我可以在訓練他們,這裏是應該的樣子:
這裏是我的解決方案:
def flatten_features(f_matrix, ID):
'''constructs a 1x(6*n) vector from 6xn matrix'''
#check wether it is a series, not dataframe
if(len(f_matrix.shape) == 1):
f_matrix['ID'] = ID
return f_matrix
flattened_vector = f_matrix.iloc[0]
for i in range(1, f_matrix.shape[0]):
vector_append = f_matrix.iloc[i]
vector_append.index = (lambda month, series_names : series_names.map(lambda name : name + '_' + str(month)))\
(i, vector_append.index)
flattened_vector = flattened_vector.append(vector_append)
flattened_vector['ID'] = ID
return flattened_vector
#construct dataframe of flattened vectors for numerical features
new_indices = flatten_features(numerical_f.iloc[:6], 1).index
new_indices
flattened_num_f = pd.DataFrame(columns=new_indices)
flattened_num_f
for label in numerical_f.index.unique():
matr = numerical_f.loc[label]
flattened_num_f = flattened_num_f.append(flatten_features(matr, label))
它產生所需的結果,但它的運行速度非常慢。我想知道,是否有更優雅和快速的解決方案?
這完全是我不清楚你的期望的輸出是什麼ID。你能舉出你的輸入例子嗎?**不是圖像**和期望的輸出? –
@ juanpa.arrivillaga我應該如何顯示我輸入的巨大df,如果不是通過jupyter筆記本表示的方式? – TheSmokingGnu
根據需要提供[mcve]。一個圖像是無用的。看看[這個問題](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples)關於如何創建一個好的,可重複的'pandas'例子。 –