열 (이것은 mongodb 추출물입니다)에 dict 목록으로 저장된 정보가 포함 된 원본 데이터 세트가 있습니다. 다음 열입니다.pandas 목록 열에서 열을 빠르게 작성합니다.
저는 처음 10 개의 dict ('p'값은 1에서 10까지)에만 관심이 있습니다 출력 dataframe은 다음과 같아야합니다
index | A | ... | B
------------------------
0 | 1 | ... | 2
1 | Nan | ... | Nan
2 | Nan | ... | 3
예컨대 : 내 원래 DataFrame의 각 라인을 위해, 나는 각 도메인 ID에 대한 열을 생성하고, 나는 'P'를 해당 값으로 연결합니다. 나는 몇 가지 'P'값에 동일한 도메인 ID를 가질 수있다이 경우에 나는 단지 더 쉬울 수도 있습니다 내 현재 코드가 이해하는 첫 번째 (작은 '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 컬럼으로 끝나는 매우 느립니다.
나는 더 빠른 해결책이 더 간단하다는 것을 확신한다.