如何使用 Python 合并或加入两个 pandas 数据框与字符串
How to use Python merge or join two pandas dataframe with string
我想合并两个数据框的数据。数据框是字符串
df1=pd.read_csv('test1.csv',encoding='utf8',index_col=['id_df1'],header=0)
df2=pd.read_csv('test2.csv',encoding='utf8',index_col=['id_df2'],header=0)
print(df1)
print(df2)
out:
id_df1 student contact_person
1 john Amy
2 jeff Cindy
3 steven Bob
4 tina Amy
id_df2 student parents_list
1 tina (Amy) (Bob)
2 steven (Eric) (Bob)
3 john (Amy)
4 jeff (Frank) (Harry)
print(type(df1['contact_person'][0]))
print(type(df2['parents_list'][0]))
out:
<class 'str'>
<class 'str'>
如果 df1['student']==df2['student'] & df1['contact_person'] 匹配 df2['parents_list']
我想像这样使用 "outer" 方法:
output
id_df1 id_df2 student contact_person parents
0 1 3 john Amy (Amy)
1 3 2 steven Bob (Eric) (Bob)
2 4 1 tina Amy (Amy) (Bob)
3 2 null jeff Cindy null
4 null 4 jeff null (Frank) (Harry)
一种方法是:首先在您的 df1
中创建一个列 bool
如果满足您的条件:
df1['bool'] = df1.apply(lambda row: True if row['contact_person'] in df2['parents_list'][df2['student'] == row['student']].iloc[0] else False,1)
那么你可以merge
满足条件的df_yes
和append
不满足的df_no
:
df_yes = df1[df1['bool'] == True].\
merge(df2, on='student', how = 'left').drop('bool',1)
df_no = df1[df1['bool'] == False].\
append(df2[df2['student'].isin(df1['student'][df1['bool'] == False])]).drop('bool',1)
最后 append
两个:
list_ordered_col = ['id_df1', 'id_df2', 'student', 'contact_person', 'parents_list']
df_output = df_yes.append(df_no)[list_ordered_col ].\
reset_index(drop=True)
注意:它使用的是您之前输入的 parent_list(当时是 a、b、c...)
编辑:将 df1['bool']=...
替换为:
def parantes_in_parentList (row, df_list):
df_parent_list = df_list['parents_list'][df_list['student'] == row['student']]
if not df_parent_list.empty:
if row['contact_person'] in df_parent_list.iloc[0]:
return True
# return False in all the other case
return False
df1['bool'] = df1.apply(parantes_in_parentList , args=([df2]),axis=1)
我想合并两个数据框的数据。数据框是字符串
df1=pd.read_csv('test1.csv',encoding='utf8',index_col=['id_df1'],header=0)
df2=pd.read_csv('test2.csv',encoding='utf8',index_col=['id_df2'],header=0)
print(df1)
print(df2)
out:
id_df1 student contact_person
1 john Amy
2 jeff Cindy
3 steven Bob
4 tina Amy
id_df2 student parents_list
1 tina (Amy) (Bob)
2 steven (Eric) (Bob)
3 john (Amy)
4 jeff (Frank) (Harry)
print(type(df1['contact_person'][0]))
print(type(df2['parents_list'][0]))
out:
<class 'str'>
<class 'str'>
如果 df1['student']==df2['student'] & df1['contact_person'] 匹配 df2['parents_list']
我想像这样使用 "outer" 方法:
output
id_df1 id_df2 student contact_person parents
0 1 3 john Amy (Amy)
1 3 2 steven Bob (Eric) (Bob)
2 4 1 tina Amy (Amy) (Bob)
3 2 null jeff Cindy null
4 null 4 jeff null (Frank) (Harry)
一种方法是:首先在您的 df1
中创建一个列 bool
如果满足您的条件:
df1['bool'] = df1.apply(lambda row: True if row['contact_person'] in df2['parents_list'][df2['student'] == row['student']].iloc[0] else False,1)
那么你可以merge
满足条件的df_yes
和append
不满足的df_no
:
df_yes = df1[df1['bool'] == True].\
merge(df2, on='student', how = 'left').drop('bool',1)
df_no = df1[df1['bool'] == False].\
append(df2[df2['student'].isin(df1['student'][df1['bool'] == False])]).drop('bool',1)
最后 append
两个:
list_ordered_col = ['id_df1', 'id_df2', 'student', 'contact_person', 'parents_list']
df_output = df_yes.append(df_no)[list_ordered_col ].\
reset_index(drop=True)
注意:它使用的是您之前输入的 parent_list(当时是 a、b、c...)
编辑:将 df1['bool']=...
替换为:
def parantes_in_parentList (row, df_list):
df_parent_list = df_list['parents_list'][df_list['student'] == row['student']]
if not df_parent_list.empty:
if row['contact_person'] in df_parent_list.iloc[0]:
return True
# return False in all the other case
return False
df1['bool'] = df1.apply(parantes_in_parentList , args=([df2]),axis=1)