2017-10-30 2 views
0

문제를 해결하는 데 논리적 인 문제가 있습니다. 두 개의 데이터 프레임이 있습니다.팬더에서 두 개의 데이터 프레임을 병합

[Id, workflowprofile_A, workflow_profile_B, important_string_info ] 

Dataframe_two이 가지고있는 다음과 같은 열 :

[workflowprofile, option, workflow] 

내 문제가 workflowprofile_A OR 및 AND Dataframe_one에서 workflow_profile_B 될 수 Dataframe_two에서 해당 workflowprofile입니다

Dataframe_one에는 다음과 같은 열이 있습니다. 열이 어떻게 생겼는지 병합 된 데이터 프레임을 어떻게 얻을 수 있습니까?

dataframe_three : 당신은 항상 하나의 값이 NaN 때문에, fillna 또는 combine_first에 의해 새 열을 만든 다음이 열을 기준으로 병합 할 수 있습니다

[Id, workflowprofile_A,workflowprofile_fromA, option_fromA, workflow_fromA,important_string_info_fromA workflow_profile_B, workflowprofile_fromB, option_fromB, workflow_fromB, important_string_info_fromB] 

답변

2

:

df1['workflowprofile'] = df1['workflowprofile_A'].fillna(df1['workflow_profile_B']) 
#alternative 
#df1['workflowprofile'] = df1['workflowprofile_A'].combine_first(df1['workflow_profile_B']) 

df3 = pd.merge(df1, df2, on='workflowprofile') 

샘플 :

print (df1) 
    Id workflowprofile_A workflow_profile_B important_string_info 
0 1    7.0     NaN      8 
1 2    NaN     5.0      1  

print (df2) 
    workflowprofile option workflow 
0    7  0   0 
1    5  9   0 
2    7  0   0 
3    4  1   2 

df1['workflowprofile'] = df1['workflowprofile_A'].fillna(df1['workflow_profile_B']) 
df3 = pd.merge(df1, df2, on='workflowprofile') 
print (df3) 
    Id workflowprofile_A workflow_profile_B important_string_info \ 
0 1    7.0     NaN      8 
1 1    7.0     NaN      8 
2 2    NaN     5.0      1 

    workflowprofile option workflow 
0    7  0   0 
1    7  0   0 
2    5  9   0 
+1

도움이됩니다. 감사! – sataide

관련 문제