美文网首页
pandas根据某列合并两个表格

pandas根据某列合并两个表格

作者: Williamongh | 来源:发表于2020-03-20 01:18 被阅读0次

之前有用过merge, join等方式, 但是个性化程度较低, 用起来不够灵活.
在使用之前, 需要把在表1的列名后面, 手动添加上表2中想要的列

df1= pd.read_csv('df1', index_col=0, encoding='gbk', dtype=str)  # type: pd.DataFrame
df2= pd.read_csv('df1', encoding='gbk', dtype=str)  # type: pd.DataFrame

for idx in df1.index:
    patient_id = df1.loc[idx, 'patient_id']  
    for col in df1.columns:
        if col in df2.columns:
            df1.loc[idx, col] = df2[df2['patient_id'] == patient_id][col].values

相关文章

网友评论

      本文标题:pandas根据某列合并两个表格

      本文链接:https://www.haomeiwen.com/subject/kshdyhtx.html