私は、dictのリストとして情報を格納した元のデータセットを列に格納しています(これはmongodbの抽出です)。これは列です:pandasリストの列から列を作成するスピードアップ
[{u'domain_id': ObjectId('A'), u'p': 1},
{u'domain_id': ObjectId('B'), u'p': 2},
{u'domain_id': ObjectId('B'), u'p': 3},
...
{u'domain_id': ObjectId('CG'), u'p': 101}]
私は最初の10 dict( 'p'の値は1から10まで)に興味があります。出力データフレームは次のようになります。
index | A | ... | B
------------------------
0 | 1 | ... | 2
1 | Nan | ... | Nan
2 | Nan | ... | 3
例えば:私のオリジナルデータフレームの各ラインについて、私は各domain_idにする列を作成し、私は「p」を対応する値に関連付けます。ここで
私は、いくつかの「P」値の同じdomain_idをを持つことができ、この場合、私は最初の1(小さい「P」)を保つ
理解しやすいかもしれ私の現在のコードです:first = True
for i in df.index[:]: # for each line of original Dataframe
temp_list = df["positions"][i] # this is the column with the list of dict inside
col_list = []
data_list = []
for j in range(10): # get the first 10 values
try:
if temp_list[j]["domain_id"] not in col_list: # check if domain_id already exist
col_list.append(temp_list[j]["domain_id"])
data_list.append(temp_list[j]["p"])
except IndexError as e:
print e
df_temp = pd.DataFrame([np.transpose(data_list)],columns = col_list) # create a temporary DataFrame for this line of the original DataFrame
if first:
df_kw = df_temp
first = False
else:
# pass
df_kw = pd.concat([df_kw,df_temp], axis=0, ignore_index=True) # concat all the temporary DataFrame : now I have my output Dataframe, with the same number of lines as my original DataFrame.
これはうまくいきますが、15k行で10k列になると非常に遅くなります。
もっと簡単な解決法があることは間違いないと思いますが、アドバイスをいただければ幸いです。