我有两个带有数据的数据框,现在我想将第二个数据框的字段合并到第一个。如果我调用第一个数据框的索引,则需要获取所有的主题名称,如下所示。谁能帮我这个?
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
答案 0 :(得分:1)
使用DataFrame.merge
,将索引按DataFrame.reset_index
转换为列,按DataFrame.set_index
转换索引:
return fetch(baseUrl, {
mode: "cors",
method: "POST",
headers: {
Host: "localhost:44370",
Allow: "GET, POST",
Accept: "application/json, text/plain",
"Content-Type": "application/json"
},
body: JSON.stringify({ Name: "tuesday", Department: "tuesday", visitorcount: 0 })
})
涉及更改数据的解决方案:
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
对于合并DataFrame 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
:
df3
仅用于filtr的最后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
行使用boolean indexing
:
Math
答案 1 :(得分: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