【问题标题】:Merging the dataframe with different indices [duplicate]合并具有不同索引的数据框[重复]
【发布时间】:2019-09-11 06:16:43
【问题描述】:

我有两个带有数据的数据框,现在我想将第二个数据框的字段合并到第一个。如果我调用第一个数据框的索引,则需要获取所有主题和主题名称,如下所示。谁能帮我这个?

import pandas as pd
sub_data = {'Subjectid':['10','11'],'Author':['Author1','Author2'],'SubjectName':['Maths', 'English']}
df1 = pd.DataFrame(sub_data)
print(df1)

topic_data = {'Topicid':['100','101','102'],'Subjectid':['10','10','11'],'TopicName':['Geometry','Trignometry', 'Tenses']}
df2 = pd.DataFrame(topic_data)
print(df2)

subtopic_data = {'Subtopicid':['1000','1001','1002'],'Topicid':['100','101','102'],'Subjectid':['10','10','11'],'SubtopicTopicName':['Lines','Angles', 'PresentTenses']}
df3 = pd.DataFrame(subtopic_data)
print(df3)

期望的输出:

    Author    SubjectName   topicid   TopicName       Subopicid SubtopicName
10  Author1       Maths     100       Geometry        1000       Lines
10  Author1       Maths     100       Trignometry     1001      Angles

【问题讨论】:

    标签: python-3.x pandas merge


    【解决方案1】:

    你可以使用合并:

    pd.merge(df1, df2, on ='Subjectid').set_index('Subjectid')
    
                Author  SubjectName Topicid TopicName
    Subjectid               
    10          Author1 Maths       100     Geometry
    10          Author1 Maths       101     Trignometry
    11          Author2 English     102     Tenses
    

    【讨论】:

    • 如果我们也有df3怎么组合?
    • 如果我只需要数学科目相关的主题和副主题(第三个)我会怎么打电话。我会尽快添加
    • @lohithdevapatla - 检查我的合并多个数据帧的答案
    【解决方案2】:

    使用DataFrame.merge 将索引转换为DataFrame.reset_index 的列和DataFrame.set_index 的列索引:

    df = df1.merge(df2.reset_index().set_index('Subjectid'), left_index=True, right_index=True)
    print (df)
         Author SubjectName index    TopicName
    10  Author1       Maths   100     Geometry
    10  Author1       Maths   101  Trignometry
    11  Author2     English   102       Tenses
    

    有问题的更改数据的解决方案:

    df = df1.merge(df2, on='Subjectid').set_index('Subjectid').rename_axis(None)
    print (df)
         Author SubjectName Topicid    TopicName
    10  Author1       Maths     100     Geometry
    10  Author1       Maths     101  Trignometry
    11  Author2     English     102       Tenses
    

    用于合并DataFrame df3:

    df = (df1.merge(df2, on='Subjectid').set_index('Subjectid')
             .merge(df3, on=['Topicid','Subjectid']))
    print (df)
        Author Subjectid SubjectName Topicid    TopicName Subtopicid  \
    0  Author1        10       Maths     100     Geometry       1000   
    1  Author1        10       Maths     101  Trignometry       1001   
    2  Author2        11     English     102       Tenses       1002   
    
      SubtopicTopicName  
    0             Lines  
    1            Angles  
    2     PresentTenses  
    

    仅用于过滤的最后一个Math 行使用boolean indexing:

    df4 = df[df['SubjectName'] == 'Maths']
    print (df4)
        Author Subjectid SubjectName Topicid    TopicName Subtopicid  \
    0  Author1        10       Maths     100     Geometry       1000   
    1  Author1        10       Maths     101  Trignometry       1001   
    
      SubtopicTopicName  
    0             Lines  
    1            Angles  
    

    【讨论】:

    • 如果我只需要 10 个索引号我的意思是只有数学科目,我该如何调用?
    • @lohithdevapatla - 我看到有问题的更改数据,你不能这样做,因为这样会使答案无效。
    • 如果我只需要数学科目和相关主题名称,不可以吗?
    • @lohithdevapatla - 是的,然后使用out = df.loc[df['SubjectName'] == 'Maths', 'TopicName']
    • @lohithdevapatla - 或out = df.loc[df['SubjectName'] == 'Maths', ['SubjectName','TopicName']]
    猜你喜欢
    • 1970-01-01
    • 2015-02-10
    • 2016-07-31
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    相关资源
    最近更新 更多